简体   繁体   中英

Android, very Simple BLE Example, unable to connect to device

At work we have a device that updates a custom characteristic in a custom service every few seconds and uses BLE. All I want to do is monitor those updates. My objective here is to write the bare minimum of code in order to connect to that device, then to the service and then monitor its characteristic. However I can't connect to the device.

Every time a bluetooth device is found by scanning this function is called:

void checkDevice(BluetoothDevice device){
    if (targetFound) return;
    if (device.getAddress().equals(DEVICE_ADDRESS)){
        logger.append("-> Target device found with name: " + device.getName() + "\n");
        targetFound = true;
        targetDevice = device;
        bleAdapter.stopLeScan(new BluetoothAdapter.LeScanCallback() {
            @Override
            public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
                logger.append("-> Scan has stopped\n");
            }
        });

        logger.append("-> Attempting to connect to target device\n");
        gattConnection = targetDevice.connectGatt(this,false,gattCallBackFuntion);
    }
}

This is my implementation of the gattCallBackFunction:

private BluetoothGattCallback gattCallBackFuntion = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        super.onConnectionStateChange(gatt, status, newState);

        if (newState == BluetoothProfile.STATE_CONNECTED) {
            Boolean gattRes = gattConnection.discoverServices();
            logger.append("-> Connected to GATT Server. Starting service discovery. Result: " + gattRes.toString() +  "\n");
        }
        else{
            logger.append("-> Connection state has changed but is " + newState);
        }
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        super.onServicesDiscovered(gatt, status);
        List<BluetoothGattService> services = gatt.getServices();
        String list = "";
        String tab = "   ";
        for (int i = 0; i < services.size(); i++){
            BluetoothGattService service = services.get(i);
            list = list + tab + service.toString() + "\n";
        }
        logger.append("-> Services Discovered: \n");
        logger.append(list);
    }
};

Both of these functions are implemented in the same class which is the only activity.

As I understand it at some point the function onConnectionStateChange must be invoked by Android indicating a connection to the Device. However this never happens. The app prints "Attempting to connect to target device" and nothign else happens

Any ideas?

Most of the Bluetooth gatt callbacks run inside a background thread hence you should not perform any UI operations inside the callback. You need to run all the operations on UI thread as per the Android UI guidelines

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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