代码之家  ›  专栏  ›  技术社区  ›  rosu alin

设置蓝牙信标设备的UUID无法正常工作

  •  1
  • rosu alin  · 技术社区  · 6 年前

    我和我的设备配对,我连接到它,使用这个代码

       //this will try  to connect to our bluetooth device
    public void connectGatt(Context context, BluetoothDevice btDevice) {
        this.btDevice = btDevice;
        mBluetoothGatt = btDevice.connectGatt(context, false, mGattCallback);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && mBluetoothGatt != null) {
            mBluetoothGatt.requestMtu(512);
        }
    }
    

     private final BluetoothGattCallback mGattCallback =
            new BluetoothGattCallback() {
                @Override
                public void onConnectionStateChange(BluetoothGatt gatt, int status,
                                                    int newState) {
                    if (newState == BluetoothProfile.STATE_CONNECTED) {
                        tryToConnect = false;
                        Log.i("BluetoothService", "BluetoothService onConnectionStateChange CONNECTED");
                        if (back != null)
                            back.onResponse(REMOVE_CALLBACKS);
                        Log.i("", "Connected to GATT server.");
                        boolean discover = mBluetoothGatt.discoverServices();
                        Log.i("", "Attempting to start service discovery:" + discover);
                    } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                        Log.i("BluetoothService", "BluetoothService onConnectionStateChange STATE_DISCONNECTED");
                        if (tryToConnect) {
                            if (back != null)
                                back.onResponse(REMOVE_CALLBACKS);
                            stopMonitoringBeacons();
                            stopListeningForBeacons();
                            mBluetoothAdapter.disable();
                            setSleep(1500);
                            mBluetoothAdapter.enable();
                            setSleep(1500);
                            Log.i("BluetoothService", "BluetoothService onConnectionStateChange WILL TRY CONNECT");
                            if (back != null)
                                back.onResponse(CONNECT);
                            tryToConnect = false;
                        }
                    }
                }
    
                @Override
                // New services discovered
                public void onServicesDiscovered(BluetoothGatt gatt, int status) {
                    tryToConnect = false;
                    if (status == BluetoothGatt.GATT_SUCCESS) {
                        Log.i("BluetoothService", "BluetoothService onConnectionStateChange onServicesDiscovered GATT_SUCCESS" + status);
                        Log.w("", "onServicesDiscovered GATT_SUCCESS: " + status);
                        List<BluetoothGattService> listBGS = mBluetoothGatt.getServices();
                        Log.i("", "list size: " + listBGS.size());
                        if (listBGS.size() > 0) {
                            if (back != null)
                                back.onResponse(CONFIGURE);
                            String character = "FF05";
                            if (justName)
                                character = "FF01";
                            setCharacteristic(gatt, character);
                        } else {
                            Log.i("BluetoothService", "BluetoothService onConnectionStateChange onServicesDiscovered GATT_SUCCESS but ZERO SERVICES: " + btDevice.getBondState());
                            if (btDevice.getBondState() == BluetoothDevice.BOND_BONDED) {
                                setSleep(500);
                                if (btDevice.getBondState() == BluetoothDevice.BOND_BONDED)
                                    mBluetoothGatt.discoverServices();
                            } else if (btDevice.getBondState() == BluetoothDevice.BOND_NONE) {
                                askPairDevice();
                            }
                        }
                    } else {
                        askPairDevice();
                        Log.w("BluetoothService", "BluetoothService onServicesDiscovered received: " + status);
                    }
                }
    
                @Override
                // Result of a characteristic read operation
                public void onCharacteristicRead(final BluetoothGatt gatt,
                                                 final BluetoothGattCharacteristic characteristic,
                                                 int status) {
                    handler.removeCallbacksAndMessages(null);
                    Log.i("BluetoothService", "BluetoothService onCharacteristicRead");
                    if (status == BluetoothGatt.GATT_SUCCESS) {
                        Log.i("BluetoothService", "BluetoothService onCharacteristicRead GATT_SUCCESS");
                        Log.w("", "BGC onCharacteristicRead GATT_SUCCESS: " + status + " / char: " + characteristic);
                        if (characteristic.getUuid().toString().toUpperCase().contains("FF05")) {
                            final String value = toHexadecimal(characteristic.getValue());
                            if (newBeacon == null || newBeacon.getName() == null) {
                                checkIfNeedsToChangeUUID(gatt, value);
                            } else if (newBeacon != null && (newBeacon.getUuid() != null || justName)) {
                                Log.i("BluetoothService", "BluetoothService new Beacon UUID is: " + value);
                                newBeacon.setUuid(Identifier.parse(value).toString());
                                if (back != null)
                                    back.onResponse(CHANGED);
                            } else {
                                changeUUID(gatt, characteristic, value);
                            }
                        } else if (characteristic.getUuid().toString().toUpperCase().contains("FF01")) {
                            changeName(gatt, characteristic);
                        }
                    } else {
                        Log.i("", "");
                    }
                }
            };
    

    我感兴趣的部分是:

       public void changeUUID(final BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, String value) {
        int size = value.length();
        RandomString gen = new RandomString(size, new SecureRandom());
        String uuid = gen.nextString();
        Log.i("", "BluetoothService BGC value NEW UUID: " + uuid);
        boolean change = characteristic.setValue(hexStringToByteArray(uuid));
        Log.i("", "BluetoothService BGC value after: " + toHexadecimal(characteristic.getValue()) + " / has changed: " + change);
        boolean statusWrite = gatt.writeCharacteristic(characteristic);
        Log.i("", "BluetoothService BGC value after statusWRITE: " + statusWrite);
        if (statusWrite) {
            newBeacon.setUuid(Identifier.parse(toHexadecimal(characteristic.getValue())).toString());
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    setCharacteristic(gatt, "FF05");
                }
            });
        } else {
            if (back != null)
                back.onResponse(ERROR);
        }
    }
    

    这就叫做:

           try {
            Log.i("BluetoothService", "BluetoothService set characteristic: " + characteristic);
            BluetoothGattCharacteristic btChar = null;
            for (BluetoothGattService bgs : gatt.getServices()) {
                for (final BluetoothGattCharacteristic bgc : bgs.getCharacteristics()) {
                    if (bgc.getUuid().toString().toUpperCase().contains(characteristic)) {
                        btChar = bgc;
                        gatt.readCharacteristic(bgc);
                        break;
                    }
                }
            }
            final BluetoothGattCharacteristic btF = btChar;
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (btF != null)
                        gatt.readCharacteristic(btF);
                }
            }, 1000);
        } catch (Exception e) {
            Log.i("BluetoothService", "BluetoothService set characteristic ERROR: " + e.getMessage());
            setCharacteristic(gatt, characteristic);
        }
    }
    

    我是说 boolean statusWrite = gatt.writeCharacteristic(characteristic); 总是向我返回“TRUE”,那么如果在“changeuid”函数中,我在另一个调用之后,尝试记录它,为什么它没有更改?

    1 回复  |  直到 6 年前
        1
  •  1
  •   davidgyoung    6 年前

    如果你看看 documentation 对于 writeCharacteristic ,您将看到:

    启动 成功地

    上面的重点是我的。仅仅因为你成功地开始写并不意味着它 完成 成功了,事实上,在我的应用程序中,我经常看到它没有成功。

    您需要做的是实现回调 public void onCharacteristicWrite (BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) ,然后检查状态是否为 BluetoothGatt.GATT_SUCCESS

    大多数其他Android Bluetooth API都是这样工作的——发现服务、发现特性等。这些异步操作中的每一个都可能失败,您必须实现回调以确定它们是否真的成功,并添加重试策略。