简体   繁体   English

我的BLE设备无法从Android接收数据

[英]My BLE device does not receive data from Android

I have a raspberry with the following characteristic (using Node.JS and Bleno library): 我有一个具有以下特征的树莓派(使用Node.JS和Bleno库):

PrinterCharacteristic.prototype.onWriteRequest = function(data, offset, withoutResponse, callback) {
    this._value = data

    console.log('onWriteRequest working with "'+ this._value + '"')
}

Also, I have my Android device that should bind with that device and send data to it. 另外,我有应该与该设备绑定并向其发送数据的Android设备。

Here is my Android code: 这是我的Android代码:

BluetoothGatt mBluetoothGatt = device.connectGatt(getActivity(), false, new BluetoothGattCallback() {

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

        if (status == BluetoothGatt.GATT_SUCCESS) {
            Log.d("BLE", "start discover");
            gatt.discoverServices();
        }
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        Log.d("BLE", "onServicesDiscovered");

        if (status == BluetoothGatt.GATT_SUCCESS) {
            BluetoothGattService mService = gatt.getServices().get(0);
            for (BluetoothGattCharacteristic bgc : mService.getCharacteristics()) {
                Log.d("BLE", "characteristic " + bgc.getUuid() + " " + bgc.toString());

                bgc.setValue("Hello World");

                Log.d("BLE", "writing characteristc");
                gatt.writeCharacteristic(bgc);
            }

        } else {
            Log.d("BLE", "onServicesDiscovered error");
        }
    }

});

Here is my Log: 这是我的日志:

09-16 13:03:46.769 18585-18585/us.inevent.apps.whiteLabel D/BluetoothGatt: connect() - device: B8:27:EB:98:BE:9D, auto: false
09-16 13:03:46.769 18585-18585/us.inevent.apps.whiteLabel D/BluetoothGatt: registerApp()
09-16 13:03:46.770 18585-18585/us.inevent.apps.whiteLabel D/BluetoothGatt: registerApp() - UUID=9a602983-99db-45fd-a3a3-2eb3ed72989a
09-16 13:03:46.772 18585-18987/us.inevent.apps.whiteLabel D/BluetoothGatt: onClientRegistered() - status=0 clientIf=6
09-16 13:03:47.192 18585-18602/us.inevent.apps.whiteLabel D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=6 device=B8:27:EB:98:BE:9D
09-16 13:03:47.193 18585-18602/us.inevent.apps.whiteLabel D/BLE: start discover
09-16 13:03:47.193 18585-18602/us.inevent.apps.whiteLabel D/BluetoothGatt: discoverServices() - device: B8:27:EB:98:BE:9D
09-16 13:03:47.249 18585-18601/us.inevent.apps.whiteLabel D/BluetoothGatt: onSearchComplete() = Device=B8:27:EB:98:BE:9D Status=0
09-16 13:03:47.249 18585-18601/us.inevent.apps.whiteLabel D/BLE: onServicesDiscovered
09-16 13:03:47.250 18585-18601/us.inevent.apps.whiteLabel D/BLE: characteristic 00002a00-0000-1000-8000-00805f9b34fb android.bluetooth.BluetoothGattCharacteristic@1b258afb
09-16 13:03:47.250 18585-18601/us.inevent.apps.whiteLabel D/BLE: writing characteristc
09-16 13:03:47.250 18585-18601/us.inevent.apps.whiteLabel D/BLE: characteristic 00002a01-0000-1000-8000-00805f9b34fb android.bluetooth.BluetoothGattCharacteristic@3876d718
09-16 13:03:47.250 18585-18601/us.inevent.apps.whiteLabel D/BLE: writing characteristc
09-16 13:03:47.533 18585-18585/us.inevent.apps.whiteLabel D/BluetoothAdapter: stopLeScan()

It found two characteristics (as expected, WRITE and NOTIFY), and tried to send data to each of them, but my raspberry does not receive any data. 它发现了两个特征(如预期的那样,WRITE和NOTIFY),并尝试向每个特征发送数据,但是我的树莓派没有收到任何数据。

If I use the nRF Connect app, it works as expected. 如果我使用nRF Connect应用程序,它将按预期工作。

What am I doing wrong? 我究竟做错了什么? I'm not sure I understood exactly how BLE works. 我不确定我是否确切了解BLE的工作原理。

Thanks. 谢谢。

It seems that Bleno has another service running. Bleno似乎正在运行另一个服务。 To solve my problem I've just searched for all characteristics inside all service and determined which one I should use by the UUID. 为了解决我的问题,我刚刚在所有服务中搜索了所有特征,并确定了UUID应该使用哪个特征。

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

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