简体   繁体   English

在某些设备上没有蓝牙gatt回调

[英]No Bluetooth gatt callback on some devices

My app lists services found on a Bluetooth LE device. 我的应用程序列出了在蓝牙LE设备上找到的服务。 It has: 它具有:

  • a service (BluetoothLeService.java) that handles gattcallbacks and broadcasts updates 处理gattcallbacks并广播更新的服务(BluetoothLeService.java)

  • a broadcast receiver that is declared in main activity 在主要活动中声明的广播接收器

I can not reliably get GATT callbacks from BluetoothGatt in my broadcast receiver. 我无法在广播接收器中从BluetoothGatt可靠地获取GATT回调。 I have 2 devices I'm testing with and they get different results for the same code. 我有2个设备正在测试,对于相同的代码,它们将获得不同的结果。 My Tablet running Android 4.4.2 gets no callback at all, while my Nexus 4 phone running 5.1.1 gets the callback and displays the services. 运行Android 4.4.2的平板电脑完全没有回调,而运行5.1.1的Nexus 4手机则获得回调并显示服务。

Here is my gatt callback from inside my service: 这是服务内部的gatt回调:

private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        String intentAction;
        if (newState == BluetoothProfile.STATE_CONNECTED) {
            intentAction = ACTION_GATT_CONNECTED;
            mConnectionState = STATE_CONNECTED;
            broadcastUpdate(intentAction);
            Log.i(TAG, "Connected to GATT server.");
            // Attempts to discover services after successful connection.
            Log.i(TAG, "Attempting to start service discovery.");
                    //mBluetoothGatt.discoverServices();
                    gatt.discoverServices();
        } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            intentAction = ACTION_GATT_DISCONNECTED;
            mConnectionState = STATE_DISCONNECTED;
            Log.i(TAG, "Disconnected from GATT server.");
            broadcastUpdate(intentAction);
        }
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {

        if (status == BluetoothGatt.GATT_SUCCESS) {
            broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
            Log.d(TAG, "Services discovered, broadcasting update");
        } else {
            Log.w(TAG, "onServicesDiscovered received: " + status);
        }
    }

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt,
                                     BluetoothGattCharacteristic characteristic,
                                     int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
        }
    }

    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt,
                                        BluetoothGattCharacteristic characteristic) {
        broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
        Log.d(TAG, "Characteristic changed, broadcasting update");


    }

    @Override
    public void onCharacteristicWrite(BluetoothGatt gatt,
                                      BluetoothGattCharacteristic characteristic,
                                      int status) {
        broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
        Log.d(TAG, "Characteristic written, braodcasting update");
    }
};

And here is what I have in my main activity: 这是我主要活动中的内容:

// Code to manage Service lifecycle.
private final ServiceConnection mServiceConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName componentName, IBinder service) {
        mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService();
        if (!mBluetoothLeService.initialize()) {
            Log.e(TAG, "Unable to initialize Bluetooth");
            finish();
        }
        // Automatically connects to the device upon successful start-up initialization.
        mBluetoothLeService.connect(mDeviceAddress);
    }

    @Override
    public void onServiceDisconnected(ComponentName componentName) {
        Log.d(TAG,"Disconnected from service");
        mBluetoothLeService = null;
    }
};
// Handles various events fired by the Service.
// ACTION_GATT_CONNECTED: connected to a GATT server.
// ACTION_GATT_DISCONNECTED: disconnected from a GATT server.
// ACTION_GATT_SERVICES_DISCOVERED: discovered GATT services.
// ACTION_DATA_AVAILABLE: received data from the device.  This can be a result of read
//                        or notification operations.

private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        final String action = intent.getAction();
        Log.d(TAG,"Received data from broadcast receiver: "+ action);

        if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {
            mConnected = true;
            updateConnectionState(R.string.connected);
            invalidateOptionsMenu();
        } else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
            Log.d(TAG,"Disconnected from the gatt server");
            mConnected = false;
            updateConnectionState(R.string.disconnected);
            invalidateOptionsMenu();
             clearUI();
        } else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            Log.d(TAG,"Receiver: Action Found");
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            // get fresh uuids
            device.fetchUuidsWithSdp();

        } else if (BluetoothDevice.ACTION_UUID.equals(action)) {
            // Get the device and teh uuids
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            Parcelable[] uuidExtra = intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);

            if (uuidExtra != null) {

                for (Parcelable uuid : uuidExtra) {
                    Log.d(TAG, "Device: " + device.getName() + " ( " + device.getAddress() + " ) - Service: " + uuid.toString());
                }

            }
        } else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
            // Show all the supported services and characteristics on the user interface.
            myDevice.setServices(mBluetoothLeService.getSupportedGattServices());
            Log.d(TAG,"Services for "+ myDevice.getName() +" set by broadcast receiver");
            displayGattServices(mBluetoothLeService.getSupportedGattServices());
        } else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {

            displayData(intent.getStringExtra(BluetoothLeService.EXTRA_DATA));
            Log.d(TAG, "Display Data: " + intent.getStringExtra(BluetoothLeService.EXTRA_DATA));
        }
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_api);

    final Intent intent = getIntent();

    final Context context = getApplicationContext();

    // Initializes a Bluetooth adapter.  For API level 18 and above, get a reference to
    // BluetoothAdapter through BluetoothManager.
    final BluetoothManager bluetoothManager =
            (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = bluetoothManager.getAdapter();


    //Intent gattServiceIntent = new Intent(context, BluetoothLeService.class);
    //context.bindService(gattServiceIntent, mServiceConnection, Context.BIND_AUTO_CREATE);
    Intent gattServiceIntent = new Intent(this, BluetoothLeService.class);
    bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
...
});

And finally, some log output of what happens when I dont get a callback 最后,一些日志输出显示当我没有得到回调时会发生什么

D/BluetoothGatt﹕ connect() - device: 00:07:80:2D:D3:5C, auto: false
D/BluetoothGatt﹕ registerApp()
D/BluetoothGatt﹕ registerApp() - UUID=468b550a-c0e6-4c54-8437-bb83d07f9be8
D/BluetoothGatt﹕ onClientRegistered() - status=0 clientIf=5
D/VXHB﹕ Trying to create a new connection.
D/BluetoothGatt﹕ onClientConnectionState() - status=133 clientIf=5 device=00:07:80:2D:D3:5C
I/VXHB﹕ Connected to GATT server.
I/VXHB﹕ Attempting to start service discovery.
D/BluetoothGatt﹕ discoverServices() - device: 00:07:80:2D:D3:5C
D/VXHB﹕ Received data from broadcast receiver: my.package.name.ACTION_GATT_CONNECTED

As you can see from the logcat output the broadcast for ACTION_GATT_CONNECTED works and discoverservices is actually fired, but I never get a callback. 正如您从logcat输出中看到的那样,ACTION_GATT_CONNECTED的广播工作正常,discoverservices实际上被触发了,但我从未收到回调。

Any clues how to troubleshoot this? 任何线索如何解决这个问题?

Try registering your broadcast receiver in onCreate as such: 尝试像这样在onCreate中注册广播接收器:

IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothLeService.ACTION_GATT_CONNECTED);
filter.addAction(BluetoothLeService.ACTION_GATT_DISCONNECTED);
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothDevice.ACTION_UUID);
filter.addAction(BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED);
filter.addAction(BluetoothLeService.ACTION_DATA_AVAILABLE);

registerReceiver(mGattUpdateReceiver, filter);

There was something elsewhere in my code causing this to malfunction. 我的代码中有其他地方导致此故障。 Reverted to an older commit and problem was solved. 恢复为较早的提交,问题已解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM