I'm unable to get UUID
of a iBeacon on pre-Lollipop devices, but below my code is working in Marshmallow Nexus 5x.
I dont want to use any library like AltBeacon or related.
BluetoothManager bm = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter mBluetoothAdapter = bm.getAdapter();
if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
//Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
//startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
} else {
BluetoothLeScanner scanner = mBluetoothAdapter.getBluetoothLeScanner();
// scan for devices
scanner.startScan(new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
// get the discovered device as you wish
// this will trigger each time a new device is found
BluetoothDevice device = result.getDevice();
if (device.getType() == BluetoothDevice.DEVICE_TYPE_LE) { //int the device type DEVICE_TYPE_CLASSIC, DEVICE_TYPE_LE DEVICE_TYPE_DUAL. DEVICE_TYPE_UNKNOWN if it's not available
if (device.fetchUuidsWithSdp()) {
System.out.println(device.getName());
List<ParcelUuid> uuids = result.getScanRecord().getServiceUuids();
System.out.println(uuids);
System.out.println(getMajor(result.getScanRecord().getBytes()));
System.out.println(getMinor(result.getScanRecord().getBytes()));
} else {
System.out.println("failed");
}
}
}
});
}
BluetoothManager bm = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter mBluetoothAdapter = bm.getAdapter();
if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
//Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
//startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
BluetoothLeScanner scanner = mBluetoothAdapter.getBluetoothLeScanner();
// scan for devices
scanner.startScan(new ScanCallback() {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void onScanResult(int callbackType, ScanResult result) {
// get the discovered device as you wish
// this will trigger each time a new device is found
BluetoothDevice device = result.getDevice();
if (device.getType() == BluetoothDevice.DEVICE_TYPE_LE)//int the device type DEVICE_TYPE_CLASSIC, DEVICE_TYPE_LE DEVICE_TYPE_DUAL. DEVICE_TYPE_UNKNOWN if it's not available
{
if (device.fetchUuidsWithSdp()) {
System.out.println(device.getName());
List<ParcelUuid> uuids = result.getScanRecord().getServiceUuids();
System.out.println(uuids);
System.out.println(getMajor(result.getScanRecord().getBytes()));
System.out.println(getMinor(result.getScanRecord().getBytes()));
} else {
System.out.println("failed");
}
}
}
});
} else {
// targetting kitkat or bellow
mBluetoothAdapter.startLeScan(new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
// get the discovered device as you wish\
}
});
}
}
public void onLeScan(BluetoothDevice device, int rssi, final byte[] scanRecord) {
int startByte = 2;
boolean patternFound = false;
while (startByte <= 5) {
if ( ((int) scanRecord[startByte + 2] & 0xff) == 0x02 && //Identifies an iBeacon
((int) scanRecord[startByte + 3] & 0xff) == 0x15) { //Identifies correct data length
patternFound = true;
break;
}
startByte++;
}
if (patternFound) {
//Convert to hex String
byte[] uuidBytes = new byte[16];
System.arraycopy(scanRecord, startByte+4, uuidBytes, 0, 16);
String hexString = bytesToHex(uuidBytes);
//Here is your UUID
String uuid = hexString.substring(0,8) + "-" +
hexString.substring(8,12) + "-" +
hexString.substring(12,16) + "-" +
hexString.substring(16,20) + "-" +
hexString.substring(20,32);
//Here is your Major value
int major = (scanRecord[startByte+20] & 0xff) * 0x100 + (scanRecord[startByte+21] & 0xff);
//Here is your Minor value
int minor = (scanRecord[startByte+22] & 0xff) * 0x100 + (scanRecord[startByte+23] & 0xff);
System.out.println(String.format("%s\n%s\n%s",uuid,major,minor));
textView.setText(String.format("%s\n%s\n%s", uuid,major,minor));
}
}
/**
* bytesToHex method
* Found on the internet
* http://stackoverflow.com/a/9855338
*/
static final char[] hexArray = "0123456789ABCDEF".toCharArray();
private static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for ( int j = 0; j < bytes.length; j++ ) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
Call the method like this and Enjoy :) onLeScan(device,result.getRssi(),result.getScanRecord().getBytes());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With