gpt4 book ai didi

cordova - 从秤设备获取蓝牙数据

转载 作者:行者123 更新时间:2023-12-04 05:30:16 24 4
gpt4 key购买 nike

我正在使用这个插件:https://github.com/don/cordova-plugin-ble-central

我可以扫描蓝牙设备,检测到我的体重秤并且我可以连接到它
但我无法从设备中获取任何数据。

使用该插件从蓝牙设备获取数据的过程是什么?

这里有一些代码错误:

 W/PluginManager( 5949): THREAD WARNING: exec() call to BLE.stopScan blocked the main thread for 24ms. Plugin should use CordovaInterface.getThreadPool().
D/BluetoothGatt( 5949): connect() - device: 7C:66:9D:9E:46:31, auto: false
D/BluetoothGatt( 5949): registerApp()
D/BluetoothGatt( 5949): registerApp() - UUID=08a761d6-34e9-4db3-8c58-258786ad7c3c
D/BtGatt.GattService( 2228): registerClient() - UUID=08a761d6-34e9-4db3-8c58-258786ad7c3c
D/BtGatt.GattService( 2228): onClientRegistered() - UUID=08a761d6-34e9-4db3-8c58-258786ad7c3c, clientIf=5
D/BluetoothGatt( 5949): onClientRegistered() - status=0 clientIf=5
D/BtGatt.GattService( 2228): clientConnect() - address=7C:66:9D:9E:46:31, isDirect=true
D/btif_config( 2228): btif_get_address_type: Device [7c:66:9d:9e:46:31] address type 0
D/btif_config( 2228): btif_get_device_type: Device [7c:66:9d:9e:46:31] type 2
D/cr_Ime ( 5949): [InputMethodManagerWrapper.java:59] isActive: true
D/cr_Ime ( 5949): [InputMethodManagerWrapper.java:68] hideSoftInputFromWindow
E/bt-btm ( 2228): Random unmatch
W/bt-btm ( 2228): btm_acl_created hci_handle=2 link_role=0 transport=2
W/bt-btif ( 2228): info:x0
W/bt-l2cap( 2228): L2CA_SetDesireRole() new:x0, disallow_switch:0
D/ ( 2228): remote version info [7c:66:9d:9e:46:31]: 0, 0, 0
D/BtGatt.GattService( 2228): onConnected() - clientIf=5, connId=5, address=7C:66:9D:9E:46:31
D/BluetoothGatt( 5949): onClientConnectionState() - status=0 clientIf=5 device=7C:66:9D:9E:46:31
D/BluetoothGatt( 5949): discoverServices() - device: 7C:66:9D:9E:46:31
D/BtGatt.GattService( 2228): discoverServices() - address=7C:66:9D:9E:46:31, connId=5
E/bt-btif ( 2228): No More Service found
D/BtGatt.GattService( 2228): onSearchCompleted() - connId=5, status=0
D/BluetoothGatt( 5949): onSearchComplete() = Device=7C:66:9D:9E:46:31 Status=0
E/PluginManager( 5949): Uncaught exception from plugin
E/PluginManager( 5949): java.lang.NumberFormatException: Invalid long: "0000null"
E/PluginManager( 5949): at java.lang.Long.invalidLong(Long.java:124)
E/PluginManager( 5949): at java.lang.Long.parse(Long.java:363)
E/PluginManager( 5949): at java.lang.Long.parsePositiveLong(Long.java:413)
E/PluginManager( 5949): at java.util.UUID.fromString(UUID.java:190)
E/PluginManager( 5949): at com.megster.cordova.ble.central.UUIDHelper.uuidFromString(UUIDHelper.java:32)
E/PluginManager( 5949): at com.megster.cordova.ble.central.BLECentralPlugin.uuidFromString(BLECentralPlugin.java:573)
E/PluginManager( 5949): at com.megster.cordova.ble.central.BLECentralPlugin.execute(BLECentralPlugin.java:163)
E/PluginManager( 5949): at org.apache.cordova.CordovaPlugin.execute(CordovaPlugin.java:117)
E/PluginManager( 5949): at org.apache.cordova.CordovaPlugin.execute(CordovaPlugin.java:98)
E/PluginManager( 5949): at org.apache.cordova.PluginManager.exec(PluginManager.java:133)
E/PluginManager( 5949): at org.apache.cordova.CordovaBridge.jsExec(CordovaBridge.java:59)
E/PluginManager( 5949): at org.apache.cordova.engine.SystemExposedJsApi.exec(SystemExposedJsApi.java:41)
E/PluginManager( 5949): at org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
E/PluginManager( 5949): at org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:39)
E/PluginManager( 5949): at android.os.Handler.dispatchMessage(Handler.java:102)
E/PluginManager( 5949): at android.os.Looper.loop(Looper.java:135)
E/PluginManager( 5949): at android.os.HandlerThread.run(HandlerThread.java:61)
W/cr_BindingManager( 5949): Cannot call determinedVisibility() - never saw a connection for the pid: 5949
D/HeadsetStateMachine( 2228): Disconnected process message: 10, size: 0

最佳答案

  • 蓝牙设备具有服务和特性,一个服务可以具有一个或多个特性。
  • 您需要读取的数据存储在某个特征中,您需要做的就是找到正确的特征来读取。
  • 连接到外围设备后,首先打印外围设备。

  • ble.connect(deviceMac,function(peripheral){
    console.log(peripheral); //print your peripheral
    },function(){
    console.log("error connecting");
    })

    在控制台中,您可以找到服务和特性。找到合适的特征后,开始通知该特征。它的代码:

    var service="ffe0"; //or appropriate uuid
    var characteristic="ffe1" //or appropriate uuid
    ble.startNotification(deviceMac,service,characteristic,function(data){
    try{
    console.log(bytesToString(data)); //convert the array buffer into readable string
    }catch(e){
    console.log(e);
    }
    },
    function(){
    console.log("error starting notifications.");
    });

    function bytesToString(buffer)
    {
    var arr = new Uint8Array(buffer);
    var str = String.fromCharCode.apply(String, arr);
    if(/[\u0080-\uffff]/.test(str))
    {
    throw new Error("this string seems to contain (still encoded) multibytes");
    }
    return str;
    }

    我认为与外围设备的连接在某处丢失,请确保在交换数据之前保持与外围设备的连接。如果你喜欢与我分享代码。

    关于cordova - 从秤设备获取蓝牙数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41332075/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com