连接至小米手环 2
Connection to Mi Band 2
使用pangliang/miband-sdk-android库无法连接到小米手环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/xiaomi-miband-android 库的情况下进行连接,但在这两种情况下都没有效果。
好像是什么问题?连接小米手环有什么技巧吗?
我发现了两件事:第一 - 我的问题不够清楚,第二 - mi band 2 有另一个 сonnection sequence 和另一个服务 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);
}
}
}
现在我们需要将一个新值写入 auth 特征:
chrt.setValue(新字节[]{0x01, 0x8, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45});
gatt.writeCharacteristic(图表);
第一个和第二个字节值用于认证,最后一个是认证密钥。
现在我们正在等待 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);
现在我们等待小米手环2的最后一个响应,当我们得到它时前三个字节必须是这样{0x10, 0x03, 0x01}
。
我们需要用小米手环 2 进行身份验证的所有步骤。希望这对某人有所帮助。
使用pangliang/miband-sdk-android库无法连接到小米手环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/xiaomi-miband-android 库的情况下进行连接,但在这两种情况下都没有效果。
好像是什么问题?连接小米手环有什么技巧吗?
我发现了两件事:第一 - 我的问题不够清楚,第二 - mi band 2 有另一个 сonnection sequence 和另一个服务 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);
}
}
}
现在我们需要将一个新值写入 auth 特征:
chrt.setValue(新字节[]{0x01, 0x8, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45}); gatt.writeCharacteristic(图表);
第一个和第二个字节值用于认证,最后一个是认证密钥。
现在我们正在等待 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);
现在我们等待小米手环2的最后一个响应,当我们得到它时前三个字节必须是这样{0x10, 0x03, 0x01}
。
我们需要用小米手环 2 进行身份验证的所有步骤。希望这对某人有所帮助。