连接到Mi Band 2
使用pangliang/miband-sdk-android lib无法连接到mi band 2。 我解开了乐队并删除了mifit应用。连接到Mi Band 2
这里是代码示例。
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/xiaomi-miband-android库,并没有在这两种情况下的影响。
什么似乎是问题?是否有任何技巧连接到mi频段?
我发现了两件事:第一 - 我的问题还不够清楚,第二频段2有另一个连接顺序和另一个服务uuids。
当我们开始扫描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(新的byte [] {0×01 ,0x31,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x40,0x41,0x42,0x43,0x44,0x45}); gatt.writeCharacteristic(chrt);
第一个和第二个字节值是auth,最后一个是auth的关键字。
现在我们正在等待onCharacteristicChanged方法中的一些响应,并且当我们到达那里时,我们必须确定它是auth特性随着右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 band 2的最后一个响应,当我们得到它前三个字节必须是这样的{0x10, 0x03, 0x01}
。
认证的所有步骤我们需要用Mi带2做。希望这可能对某人有帮助。
你能分享一些关于Mi Band 2的API英文文档吗?我试图找到在线,但没有任何成功 – Royz