我现在有一个方法,它可以写入BLE设备来发出哔哔声。我的蓝牙回拨如下:

public class ReadWriteCharacteristic extends BluetoothGattCallback {
    public ReadWriteCharacteristic(Context context, String macAddress, UUID service, UUID characteristic, Object tag, Activity activity) {
        mMacAddress = macAddress;
        mService = service;
        mCharacteristic = characteristic;
        mTag = tag;
        mContext = context;
        this.activity =activity;
        final BluetoothManager bluetoothManager =
                (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE);
        mBluetoothAdapter = bluetoothManager.getAdapter();

    }

    final private static String TAG = "ReadCharacteristic";
    private Object mTag;
    private String mMacAddress;
    private UUID mService;
    private BluetoothManager mBluetoothManager = null;
    private BluetoothAdapter mBtAdapter = null;
    private BluetoothGatt mBluetoothGatt = null;
    private String mBluetoothDeviceAddress ="";
    private UUID mCharacteristic;
    BluetoothDevice device;
    private Activity activity;
    private BluetoothAdapter mBluetoothAdapter;
    private Context mContext;
    ReadWriteCharacteristic rc;

    private int retry = 5;


    public String getMacAddress() {
        return mMacAddress;
    }

    public UUID getService() {
        return mService;
    }

    public UUID getCharacteristic() {
        return mCharacteristic;
    }

    public byte[] getValue() { return mValue; }

    public void onError() {
        Log.w(TAG, "onError");
    }

    public void readCharacteristic(){
        if (retry == 0)
        {
            onError();
            return;
        }
        retry--;


                device = mBluetoothAdapter.getRemoteDevice(getMacAddress());


                mBluetoothManager = (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE);
                int connectionState = mBluetoothManager.getConnectionState(device,
                        BluetoothProfile.GATT);

                if (device != null) {

                    if (connectionState == BluetoothProfile.STATE_DISCONNECTED)
                    {

                        // Previously connected device. Try to reconnect.
                        if (mBluetoothDeviceAddress != null
                                && getMacAddress().equals(mBluetoothDeviceAddress)
                                && mBluetoothGatt != null) {
                            Log.w(TAG, "Re-use GATT connection");
                            if (mBluetoothGatt.connect()) {
                                Log.w(TAG, "Already connected, discovering services");
                                mBluetoothGatt.discoverServices();
                                //return ;
                            } else {
                                Log.w(TAG, "GATT re-connect failed.");
                                return;
                            }
                        }

                        if (device == null) {
                            Log.w(TAG, "Device not found.  Unable to connect.");
                            return;
                        }
                        Log.w(TAG, "Create a new GATT connection.");
                        rc= ReadWriteCharacteristic.this;
                        Log.w(TAG, "Starting Read [" + getService() + "|" + getCharacteristic() + "]");
                        mBluetoothGatt = device.connectGatt(mContext, false, rc);
                        refreshDeviceCache(mBluetoothGatt);
                        mBluetoothDeviceAddress = getMacAddress();
                    } else {
                        Log.w(TAG, "Attempt to connect in state: " + connectionState);
                        if(mBluetoothGatt!=null)
                            mBluetoothGatt.close();
                        readCharacteristic();
                    }

                }

    }

    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        super.onConnectionStateChange(gatt, status, newState);

        Log.w(TAG,"onConnectionStateChange [" + status + "|" + newState + "]");


        if ((newState == 2)&&(status ==0)) {
            gatt.discoverServices();
        }
        else if(status == 133 )
        {
            //gatt.disconnect();
            gatt.close();
            mBluetoothGatt = null;
            try
            {
                Thread.sleep(2000);
            }
            catch(Exception e)
            {

            }
            readCharacteristic();
        }
        else{

            if(mBluetoothGatt!=null)
                mBluetoothGatt.close();

           // gatt.close();

            Log.w(TAG, "[" + status + "]");
            //gatt.disconnect();

            try
            {
                Thread.sleep(2000);
            }
            catch(Exception e)
            {

            }
            mBluetoothGatt = null;
            readCharacteristic();
        }
    }



    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        Log.w(TAG,"onServicesDiscovered [" + status + "]");
        BluetoothGattService bgs = gatt.getService(getService());
        if (bgs != null) {
            BluetoothGattCharacteristic bgc = bgs.getCharacteristic(getCharacteristic());
            gatt.readCharacteristic(bgc);
        }
    }

    @Override
    public void onCharacteristicWrite(BluetoothGatt gatt,
                                      BluetoothGattCharacteristic characteristic,
                                      int status) {
        Log.w(TAG,"onCharacteristicWrite [" + status + "]");
        if (status == BluetoothGatt.GATT_SUCCESS) {
            Log.w(TAG,"onCharacteristicWrite [" + getDataHex(characteristic.getValue()) + "]");
           // gatt.disconnect();
            if(mBluetoothGatt!=null)
                mBluetoothGatt.close();
          //  gatt.close();
          //  mBluetoothGatt=null;
        }
        else if(status ==133)
        {
            gatt.close();
            try
            {
                Thread.sleep(2000);
            }
            catch(Exception e)
            {

            }

            readCharacteristic();
        }

        else{
            //gatt.disconnect();
            gatt.close();

        }
    }

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt,
                                     BluetoothGattCharacteristic characteristic,
                                     int status) {
        Log.w(TAG,"onCharacteristicRead [" + status + "]");
        if (status == BluetoothGatt.GATT_SUCCESS) {
            mValue = characteristic.getValue();
            // Perform write operations
            gatt.writeCharacteristic(bgc);


        }

        else if(status ==133)
        {
            gatt.close();
            try
            {
                Thread.sleep(2000);
            }
            catch(Exception e)
            {

            }

            readCharacteristic();
        }

        else {
          //  gatt.disconnect();
            gatt.close();
        }
    }

}

此代码在运行kitkat及以下版本的设备上运行得非常好。但在运行棒棒糖的设备上,这段代码在第一次运行时效果良好。但从下一个实例来看,不管我是断开连接还是关闭连接并重试,它都不起作用。它一直在onconnectionstatechange方法中给我一个257的状态代码。据我所知,蓝牙gatt方法对kitkat和棒棒糖设备都是一样的。
令我惊讶的是,当我使用旧的ble api即startedscan(用于eg-mBluetoothAdapter.startLeScan(mLeScanCallback);)时,这段代码在棒棒糖设备上运行良好。这个问题只在我使用新的ble api时出现,即bluetoothlescanner(scanner.startScan(filters, settings, new scancallback());)。对于使用旧的ble api的棒棒糖设备,扫描速度非常慢,因此我不能使用它。我就是不明白怎么解决这个问题。有没有人面对同样的问题,找到了解决办法?任何帮助都将不胜感激。

最佳答案

我在这里会改变很多事情。为要从特征中读取的数据创建类变量,例如私有字符串心率;
1)不需要readCharacteristic()方法。相反,在onconnectionstatechange中,一旦设备连接正确,就调用mbluetothgatt.discoverservices()。然后在onServicesDiscovered()方法中调用gatt.getServices()。然后使用foreach循环并遍历返回的服务,并比较服务的uuid,直到找到您关心的那个。然后,如果heartrate==null,则调用service.getCharacteristic(heartrateUuid),然后读取特征。在onCharacteristicRead()中,检查uuid是否等于心率特征。如果是,则将特征值赋给心率变量。如果您感兴趣,我可以键入方法或提供伪代码。
2)我不会调用gatt.connect(),然后调用gatt.discoverservices()。gatt.connect()将在看到来自当前设备的播发数据包时立即重新连接到该设备。我将调用gatt.connect(),然后在onconnectedstatechange()方法中调用gatt.discoverservices()。
3)在onconnectedstatechange方法中,不要使用gatt变量。改用MBluetothGatt。mbluetothgatt.disconnect()断开与当前连接设备的连接。mbluetothgatt.close()终止gatt实例。在调用mbluetothgatt.close()之后,不能调用mbluetothgatt.connect()。这可能不需要,但如果设备已连接,我将调用mbluetothgatt.disconnect(),然后调用mbluetothgatt.close()。
4)您也可以将特征读数连在一起。在onCharacteristicRead()中,获取心率值后,可以立即调用characteristic.getService().getCharacteristic(uuidTemperature),然后读取该特征。它将再次调用onCharacteristicRead方法。
如果你想让我澄清什么,请告诉我;我在用蹩脚的Surface Pro 3键盘打字。:)

08-26 07:40