}
});
如果扫描不出来,可将 targetSdk 调到低于 6.0.
可以随时停止扫描:
mClient.stopSearch();
蓝牙开关
打开关闭蓝牙:
mClient.openBluetooth();
mClient.closeBluetooth();
判断蓝牙是否打开:
mClient.isBluetoothOpened();
蓝牙打开或关闭需要一段时间,可以注册回调监听状态,回调的参数如果是 true 表示蓝牙已打开,false 表示蓝牙关闭
mClient.registerBluetoothStateListener(mBluetoothStateListener);
private final BluetoothStateListener mBluetoothStateListener = new BluetoothStateListener() {
@Override
public void onBluetoothStateChanged(boolean openOrClosed) {
}
};
mClient.unregisterBluetoothStateListener(mBluetoothStateListener);
设备配对
监听设备配对状态变化
private final BluetoothBondListener mBluetoothBondListener = new BluetoothBondListener() {
@Override
public void onBondStateChanged(String mac, int bondState) {
// bondState = Constants.BOND_NONE, BOND_BONDING, BOND_BONDED
}
};
mClient.registerBluetoothBondListener(mBluetoothBondListener);
mClient.unregisterBluetoothBondListener(mBluetoothBondListener);
Beacon 解析
可以在广播中携带设备的自定义数据,用于设备识别,数据广播,事件通知等,这样手机端无需连接设备就可以获取设备推送的数据。
扫描到的 beacon 数据为 byte[],在 SearchResult 的 scanRecord 中,按如下形式生成 Beacon 对象,
Beacon beacon = new Beacon(device.scanRecord);
Beacon 数据结构如下:
public class Beacon {
public byte[] mBytes;
public Li