- UUID.fromString("00001234-0000-1000-8000-00805f9b34fb")
- UUID.fromString("00007777-0000-1000-8000-00805f9b34fb")
- UUID.fromString("00009999-0000-1000-8000-00805f9b34fb")
- bluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
- List<ScanFilter> filters = new ArrayList<>();
- ScanFilter filter = new ScanFilter.Builder().setServiceUuid(ParcelUuid.fromString("00007777-0000-1000-8000-00805f9b34fb");)
- .build();
- filters.add(filter);
- ScanSettings scanSettings = new ScanSettings.Builder()
- .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
- .build();
- bluetoothLeScanner.startScan(filters, scanSettings, scanCallback);
new ScanFilter.Builder().setServiceUuid(ParcelUuid.fromString("00007777-0000-1000-8000-00805f9b34fb");
- AdvertiseData.Builder()
- .setIncludeDeviceName(true)
- .addServiceUuid(ParcelUuid.fromString("00007777-0000-1000-8000-00805f9b34fb"))
- .addManufacturerData(0x7777, new byte[]{0x07, 0x07})
- .build();
- final ScanCallback scanCallback = new ScanCallback() {
- @Override
- public void onScanResult(int callbackType, ScanResult result) {
- super.onScanResult(callbackType, result);
- ScanRecord scanRecord = result.getScanRecord();
- SparseArray<byte[]> mandufacturerData = scanRecord.getManufacturerSpecificData();
此时可以根据 mandufacturerData 来匹配自己设定的外围设备
在 BluetoothGattCallback 中的关于此问题有三步回调
1、
- public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState)
都会生成新的对象,而不会去主动关闭老的对象
- mBluetoothGatt = device.connectGatt(SpeakerApp.appContext, false, mGattCallBack);
- //iPhone 和 某些Android手机作为旁支会出现蓝牙初始连接就是133,此情况下立刻重试
- if (status == 133) {
- RLog.d(TAG, "发生设备初始连接133情况,需要重新扫描连接设备");
- mBluetoothGatt.close();
- //!!!需要去增加代码进行重新扫描重连
- return;
- }
- if (newState == BluetoothProfile.STATE_CONNECTED) {
- mBluetoothGatt = gatt;
- mBluetoothGatt.discoverServices();
- } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
- mBluetoothGatt.close();
- mBluetoothGatt = null;
- }
2、
- public void onServicesDiscovered(BluetoothGatt gatt, int status)
- mCharacteristic = service.getCharacteristic(UUID.fromString("00007770-0000-1000-8000-00805f9b34fb"));
- if (mCharacteristic == null) {
- RLog.e(TAG, "Can't find target characteristic.");
- return;
- }
- gatt.setCharacteristicNotification(mCharacteristic, true);
- BluetoothGattDescriptor descriptor = mCharacteristic.getDescriptor(UUID.fromString("00007777-0000-1000-8000-00805f9b34fb"));
- descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
- gatt.writeDescriptor(descriptor);
3、
只有这一步 status == BluetoothGatt.GATT_SUCCESS,才可以真正的传输数据,如果在第一步或者第二步就开始传输数据,会在某些特定的 case 下导致未知的 bug 或者空指针错误
- public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status)
所以,在中心设备跟外围开始连接后,你可以设定一个超时时间,在超时时间过后,依然没能回调 onDescriptorWrite 并获得 BluetoothGatt.GATT_SUCCESS,则此次过程失败,你可以根据实际情况进行重连或者提示错误
mtu20 的来源:GATT 是基于 ATT Protocol 的,而它的 core spec 里面定义了 ATT 的默认 MTU 为 23 个 bytes,除去 ATT 的 opcode 一个字节以及 ATT 的 handle2 个字节之后,剩下的 20 个字节便是留给 GATT 的了
如果要传输大于 20 字节的数据怎么办?
1、 系统 mtu 可以支持修改到 512 字节,完成大数据量的传输。但是由于涉及到中心和旁支都需要修改,会造成很大的局限性和底层修改量,而且会触发比如某些设备第一次修改不生效,另一个设备一次连接中只能修改一次等 bug,非常不可取,十分不建议。
2、分包传输,自己设计协议分包传输是最可取的方案,需要注意的是在分包后,每一个包之间写入数据需要设置间隔,比如 100ms。
- return ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE) > 0 ||
- (characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) > 0);
在做好 5 和 6 的基础上,依然会在一些设备上出现,由于系统原因,ble 刚开始的发送第一个数据出现丢包,请对此做出特殊处理。
接收到数据并还原成原始数据
- public void onCharacteristicWriteRequest(BluetoothDevice device, int requestId, BluetoothGattCharacteristic characteristic, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value)
接收到数据并还原成原始数据
- public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic)
- BluetoothDevice remoteDevice = mBluetoothAdapter.getRemoteDevice("另一个ble 或者 蓝牙设备mac值");
- if (remoteDevice.getBondState() == BluetoothDevice.BOND_BONDED) {
- try {
- Method removeBond = remoteDevice.getClass().getDeclaredMethod("removeBond");
- removeBond.invoke(remoteDevice);
- RLog.d(TAG, "成功移除系统bug");
- } catch(Exception e) {
- RLog.e(TAG, "反射异常");
- }
- }
来源: http://www.jianshu.com/p/2fd90849d8e0