从体重秤设备获取蓝牙数据

4

我正在使用这个插件: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

亲爱的kratosphere,能否提供你尝试连接并读取设备数据的代码片段? - aksonlyaks
1个回答

0
  1. 蓝牙设备具有服务和特征,一个服务可以有一个或多个特征。
  2. 您需要读取的数据存储在某些特征中,您只需要找到正确的特征进行读取。
  3. 连接后首先打印外围设备。
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;
}

我认为与外围设备的连接在某个地方丢失了,确保在交换数据之前保持与外围设备的连接。如果您愿意,可以与我分享代码。


你好,感谢你的回答。我添加了正确的服务和特征ID,并在输出日志中得到了以下结果:01-05 17:47:53.736 26344 26356 W CordovaPlugin: 结果是:{"name":"Samico Scales","id":"7C:66:9D:9E:46:31","errorMessage":"Peripheral Disconnected"} 01-05 17:47:53.736 26344 26356 D BluetoothGatt: cancelOpen() - 设备:7C:66:9D:9E:46:31。 - kratosphere
在控制台JS中出现以下错误:Success callbackId: BLE1615781344 中的错误:Error: 此字符串似乎包含(仍被编码的)多字节。 - kratosphere
你好,我认为代码中存在连接问题,你的设备会断开连接。 - Rajat Sawant

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接