连接Mi Band 2

Don*_*nic 8 java android android-bluetooth

无法使用pangliang/miband-sdk-android lib连接到mi band 2.我取消了乐队的配对并删除了mifit app.

这是代码示例.

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);
Run Code Online (Sandbox Code Playgroud)

日志:

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
Run Code Online (Sandbox Code Playgroud)

Android版本6.0.1.

此外,我尝试连接没有任何额外的库和paulgavrikov/xiaomi-miband-android库,并且在两种情况下都没有效果.

什么似乎是问题?连接到mi乐队有什么技巧吗?

Don*_*nic 9

我发现了两件事:第一件 - 我的问题不够明确,第二部分 - 米带2有另一个сonnection序列和另一个服务uuid​​s.

当我们开始扫描BT设备时,我们使用ScanCallback.当我们在onScanResult方法中获得某些东西时,我们可以尝试连接到该设备,在这种情况下我们需要使用GattCallback.

现在我们需要找到UUID为"00000009-0000-3512-2118-0009af100700"的auth特征.

当我们找到它时,我们需要在其上启用通知:

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);
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

现在我们需要为auth特性写一个新值:

chrt.setValue(new byte [] {0x01,0x8,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x40,0x41,0x42,0x43,0x44,0x45}); gatt.writeCharacteristic(CHRT);

第一个和第二个字节值用于auth,最后一个是auth的密钥.

现在我们正在等待onCharacteristicChanged方法中的一些响应,当我们到达那里时,我们必须确保它是使用正确的UUID更改的auth特性.之后,我们得到它的价值byte[] value = characteristic.getValue();

我们得到的前三个字节必须是这样的{0x10, 0x01, 0x01},如果没问题,我们会写另一个请求:

characteristic.setValue(new byte[]{0x02, 0x8});
gatt.writeCharacteristic(characteristic);
Run Code Online (Sandbox Code Playgroud)

我们得到的前三个字节必须是这样的{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);
Run Code Online (Sandbox Code Playgroud)

现在我们等待mi band 2的最后一个响应,当我们得到它时,前三个字节必须是这样的{0x10, 0x03, 0x01}.

认真的所有步骤我们需要与Mi band 2做.希望这可能对某人有所帮助.