Search code examples
javamacosbluetooth-lowenergycore-bluetoothjna

MacOS link library containing CoreBluetooth framework


I'm attempting to use Java Native Access (JNA) to provide a Java interface to the CoreBluetooth framework that's an integral part MacOS (version 12.1, in my case).

It appears that I need access to a MacOS link library in order for JNA to determine the elements of the interface that are available. However, I can't seem to locate the link library file that provides the classes, methods, etc. for this framework.

I've looked inside the following bundles with no success:

  • /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/CoreBluetooth.framework
  • /System/Library/Frameworks/CoreBluetooth.framework
  • /System/Library/PrivateFrameworks/CoreBluetoothUI.framework

I've scanned the contents of /usr/lib too, but I'm not seeing anything related to CoreBluetooth, and I don't want to have to look at the contents of every .dylib file on the system.

Any suggestions where I can find this file? Or are there any other ways I can setup a JNA interface to CoreBluetooth?


Solution

  • You can just load the library by name in JNA as usual (version 5.6 or greater).

    public interface CoreBluetooth extends Library {
    
        CoreBluetooth INSTANCE = Native.load("CoreBluetooth", CoreBluetooth.class);
    
        // mappings
    }
    

    You won't find it in the filesystem in macOS 11 or later, however. From the macOS Big Sur 11.0.1 Release Notes:

    New in macOS Big Sur 11.0.1, the system ships with a built-in dynamic linker cache of all system-provided libraries. As part of this change, copies of dynamic libraries are no longer present on the filesystem. Code that attempts to check for dynamic library presence by looking for a file at a path or enumerating a directory will fail. Instead, check for library presence by attempting to dlopen() the path, which will correctly check for the library in the cache.

    JNA 5.6 was updated to use this new behavior.

    That said, I do have a copy of the framework, or at least the header files, on my system under the following path:

    /Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/System/Library/Frameworks/CoreBluetooth.framework/
    

    That's not useful for loading into JNA but it does provide header files if the online documentation is insufficient. I'm not sure why you don't have it in your own Command Line tools path or how my installation differs from yours.