简体   繁体   中英

Android Bluetooth service discovery

I'm trying to build an app that discovers all the devices in range that are running the same app. I have done all the discovery etc, but I don't know how to filter out the ones that are running my app, without connecting to them . Just from the results of the discovery. Is that possible? And in what way? I was looking into UUIDs but I don't know enough to be able to tell if they can be utilised for that purpose, and how. Thanks in advance!

I don't know how to filter out the ones that are running my app

It depends on what flavor of Android you want to run under.

Android 4.4 and up will support Bluetooth scan filtering by the 128-bit UUID

The following are code excerpts.

private static final String CT_UUID = "Your specific 128-bit UUID string";  
.........  
String myOSVersion = android.os.Build.VERSION.RELEASE; // e.g. myOSVersion := "4.2"
float myOSVersionNum = Float.valueOf(myOSVersion.substring(0,3));  // Example 4.3, 4.4, etc.  
.........  
if (myOSVersionNum < 4.4) {
    // --- Android 4.3 (NO UUID Filtering) ---
    mBluetoothAdapter.startLeScan(mLeScanCallback);
} else {
    // --- Android 4.4 or Later (With UUID Filtering) ---
    UUID[] uuids = new UUID[1];
    uuids[0] = UUID.fromString(CT_UUID);
    mBluetoothAdapter.startLeScan(uuids, mLeScanCallback);
}

But for Android versions earlier, that will not work.
There are a number of postings here regarding what alternatives MIGHT work for Android 4.3 and earlier.

For my own needs, where Android 4.3 was involved, doing a post-acquire device-name filtering was sufficient/adequate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM