private UUID[] RX_SERVICE_UUID = new UUID[1];
RX_SERVICE_UUID[0] = UUID.fromString("0000e4eee-1112-1111-1111-14addb4as215");
当我将函数与UUID mBluetoothAdapter.startLeScan(RX_SERVICE_UUID,mLeScanCallback)
方法一起使用时。当我在没有UUID的情况下使用mBluetoothAdapter.startLeScan(mLeScanCallback)
时,它不是在扫描设备,而是在扫描设备。请提出建议。
答案 0 :(得分:-1)
我认为您正在测试棉花糖中的应用
扫描任何ble设备需要一定的权限
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-feature android:name="android.hardware.bluetooth_le" />
启用蓝牙
// Ensures Bluetooth is enabled on the device. If Bluetooth is not currently enabled,
// fire an intent to display a dialog asking the user to grant permission to enable it.
if (!mBluetoothAdapter.isEnabled()) {
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
// Initializes list view adapter.
mLeDeviceListAdapter = new LeDeviceListAdapter();
mLeDevicePairedListAdapter = new LeDevicePairedListAdapter();
setListAdapter(mLeDeviceListAdapter);
setPairedListAdapter(mLeDevicePairedListAdapter);
scanLeDevice(true);
使用此方法开始扫描
private void scanLeDevice(final boolean enable) {
if (enable) {
mScanning = true;
selectedPositions = new ArrayList<>();
pairselectedPositions = new ArrayList<>();
mBluetoothAdapter.startLeScan(mLeScanCallback);
} else {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
// invalidateOptionsMenu();
}
回叫响应
//设备扫描回调。
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, final int rssi, byte[] scanRecord) {
runOnUiThread(new Runnable() {
@Override
public void run() {
//if device bounded then added another list and not bound then using other list diplay in list
if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
mLeDevicePairedListAdapter.addDevice(device, rssi);
} else {
mLeDeviceListAdapter.addDevice(device, rssi);
}
}
});
}
};