BLE 4.0 getting the broadcast data from device to phone

后端 未结 2 1889
Happy的楠姐
Happy的楠姐 2020-12-11 08:55

\"enter

I have two devices. one Android phone with API level more than 18 and other is

相关标签:
2条回答
  • 2020-12-11 09:04

    saw your email, I think you are somehow connected via Bluetooth classic, but are then attempting to 'chat' on BTLE protocol.

    That's the problem. There is almost no way an Android 4.0 device has BTLE.

    Even if it has an BTLE chip(there was some early Motorola phones with BTLE - you had to import a .jar from Motorola Inc.), it wouldn't use the Android BTLE API you seem to use.

    So to make a long story short, you should either be using Bluetooth Classic (SPP) with the normal BluetoothSocket, or be using two Android BTLE devices.

    Here is how to check if the devices have BTLE:\

    If you want to declare that your app is available to BLE-capable devices only, include the following in your app's manifest:

    However, if you want to make your app available to devices that don't support BLE, you should still > include this element in your app's manifest, but set required="false". Then at run-time you can determine BLE availability by using PackageManager.hasSystemFeature():

    // Use this check to determine whether BLE is supported on the device. 
    // Then you can selectively disable BLE-related features.
    if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
        Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
        finish();
    }
    
    0 讨论(0)
  • 2020-12-11 09:23

    Two things I see that you should be doing.

    1. BluetoothGatt has its own setCharacteristicNotification method. In addition to writing the characteristic descriptor to enable notifications, you need to call that method to enable notifications. Think of it as writing the descriptor enables notifications on the BLE device and setCharacteristicNotification enables it on the Android device.

    So in your setCharacteristicNotification method above I would add the following:

    // I'm assuming you have access to the BluetoothGatt object in your BluetoothGattService object
    gatt.setCharacteristicNotification(characteristic, true);
    
    1. You shouldn't be trying to write any data to the characteristic until you have received confirmation that the descriptor was written. That means you need to wait until you get the callback to onDescriptorWrite in your implementation of BluetoothGattCallback.
    0 讨论(0)
提交回复
热议问题