2017-01-01 8 views
2

pangliang/miband-sdk-android libを使用してmiバンド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バンドに接続するためのトリックはありますか?

答えて

3

私は2つのことを発見しました。最初は私の質問が十分ではなく、2番目のバンド2は別の接続シーケンスともう1つのサービスuui​​dsを持っています。

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); 
      } 
     } 
    } 

今、私たちは特性のauthする新しい値を記述する必要があります。

chrt.setValue(新しいバイト[] {0x01で、0x8、0x30、0x31、0x32、0x33、0x34、0x35、0x36、0x37、0x38、0x39、0x40、0x41、0x42、0x43、0x44、0x45} gatt.writeCharacteristic(chrt);

最初の値と2番目の値はauthの値で、最後の値はauthのキーです。

私たちはonCharacteristicChangedメソッドで何らかの応答を待っています。そこで、正しいUUIDでauth特性が変更されたことを確認する必要があります。その後、私たちはその値を取得byte[] value = characteristic.getValue();

我々はこの{0x10, 0x01, 0x01}ようにする必要があり得るし、それがOKであれば、我々は別の要求書く最初の3バイト:最初の3つのバイトは、我々が得る

characteristic.setValue(new byte[]{0x02, 0x8}); 
gatt.writeCharacteristic(characteristic); 

を応答して、この{0x10, 0x02, 0x01}ようになり、それがOKであれば、我々は別の要求を書きますが、今、我々は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からの最後の応答を待つので、最初の3バイトはこのようにする必要があります{0x10, 0x03, 0x01}

私たちはMiバンド2と関係があると思います。これが誰かにとって参考になると願っています。

+0

Mi Band 2に関するAPIの英語のドキュメントを教えてください。私はオンラインでは何も成功しなかった – Royz

関連する問題