无法连接到mi波段2使用pangliang/miband-sdk-android .我取消了乐队,并删除了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=6Android版本6.0.1
此外,我试图连接没有任何额外的lib和与paulgavrikov/小米-miband-android库,并没有任何影响在这两种情况下。
什么地方出问题了?有什么窍门可以连接到mi乐队吗?
发布于 2017-03-13 14:36:43
我发现了两件事:第一,我的问题不够清楚,第二,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[]{0x01,0x8,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x40,0x41,0x42,0x43,0x44,0x45});
第一个字节值和secon字节值用于auth,最后一个字节值是auth的关键。
现在我们在等待onCharacteristicChanged方法中的一些响应,当我们到达那里时,我们必须确定它的特性随右UUID的变化而改变。之后,我们得到它的值byte[] value = characteristic.getValue();。
我们得到的前三个字节必须类似于这个{0x10, 0x01, 0x01},如果可以的话,我们编写另一个请求:
characteristic.setValue(new byte[]{0x02, 0x8});
gatt.writeCharacteristic(characteristic);响应得到的前三个字节必须类似于这个{0x10, 0x02, 0x01},如果可以的话,我们会编写另一个请求,但是现在我们需要使用AES密码:
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}。
所有的步骤,我们需要做的Mi乐队2。希望这可能会有帮助的人。
https://stackoverflow.com/questions/41417747
复制相似问题