我正在使用此插件: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
答案 0 :(得分: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;
}
我认为与外围设备的连接在某处丢失了,请确保在交换数据之前保持与外围设备的连接。如果你愿意和我分享代码。