使用pangliang / miband-SDK-的Android LIB无法连接到MI带2。我不成对的频带和删除mifit应用。
下面是代码示例。
final MiBand miband = new MiBand(TestActivity.this.getApplicationContext());
final ScanCallback scanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
BluetoothDevice device = result.getDevice();
miband.connect(device, new ActionCallback() {
@Override
public void onSuccess(Object data) {
}
@Override
public void onFail(int errorCode, String msg) {
}
});
}
};
MiBand.startScan(scanCallback);
MiBand.stopScan(scanCallback);
日志:
D/BluetoothLeScanner: Start Scan
D/BluetoothAdapter: STATE_ON
D/BluetoothAdapter: STATE_ON
D/BluetoothAdapter: STATE_ON
D/BluetoothAdapter: STATE_ON
D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=6
Android版本6.0.1。
另外,我想没有任何附加库,并与paulgavrikov /小米-miband-的Android库连接,并且在这两种情况下没有效果。
似乎是什么问题?有没有什么技巧,以连接到MI乐队?
我发现两件事情:第一 - 我的问题是不够清晰,二 - MI带2具有另一сonnection序列和其他服务的UUID。
当我们为一个BT设备开始扫描,我们使用ScanCallback。当我们在onScanResult法的东西,我们可以尝试连接到该设备,我们需要在这种情况下使用GattCallback。
现在,我们需要找到具有UUID“00000009-0000-3512-2118-0009af100700”权威性特征。
当我们发现它时,我们需要启用它通知:
private void enableNotifications(BluetoothGattCharacteristic chrt) {
bluetoothGatt.setCharacteristicNotification(chrt, true);
for (BluetoothGattDescriptor descriptor : chrt.getDescriptors()){
if (descriptor.getUuid().equals(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"))) {
Log.i("INFO", "Found NOTIFICATION BluetoothGattDescriptor: " + descriptor.getUuid().toString());
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
}
}
}
现在,我们需要写一个新的价值权威性特点:
chrt.setValue(新字节[] {0×01,0x8中,的0x30,0X31,0x32,0x33,0x34,0x35,0x36,0x37符号,0x38,0x39,0x40的,0×41,的0x42 0x43中,0×44,0×45}); gatt.writeCharacteristic(CHRT);
第一和第二个字节的值是用于身份验证,最后他们是权威性的关键。
现在,我们正在等待在onCharacteristicChanged方法的一些反应,当我们到达那里,我们必须确保它是右UUID改变身份验证的特性。之后,我们得到它的价值byte[] value = characteristic.getValue();
前三个字节我们得到的一定是这样{0x10, 0x01, 0x01}
,如果它是确定的,我们写另一个请求:
characteristic.setValue(new byte[]{0x02, 0x8});
gatt.writeCharacteristic(characteristic);
前三个字节我们得到的响应必须是这样的{0x10, 0x02, 0x01}
,如果它是确定的,我们写另一个请求,但现在我们需要使用AES chipher:
byte[] value = characteristic.getValue();
byte[] tmpValue = Arrays.copyOfRange(value, 3, 19);
Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
// here we use key like in our firt requst
SecretKeySpec key = new SecretKeySpec(new byte[] {0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45}, "AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] bytes = cipher.doFinal(tmpValue);
byte[] rq = ArrayUtils.addAll(new byte[]{0x03, 0x8}, bytes);
characteristic.setValue(rq);
gatt.writeCharacteristic(characteristic);
现在,我们等待MI带2的最后反应,当我们得到它的前三个字节必须是这样的{0x10, 0x03, 0x01}
。
这权威性的所有步骤,我们需要弥乐队2.希望能够做到这可能是别人有帮助的。