无法读取未定义的属性“ bluetoothGattService”

时间:2018-10-15 17:04:56

标签: bluetooth-lowenergy nativescript adafruit

我正在开发Nativescript蓝牙应用程序。我正在使用插件:https://market.nativescript.org/plugins/nativescript-bluetooth

我能够扫描附近的设备并发现服务。我正在尝试将“写入”的服务/功能附加到我的应用程序上的按钮。当我单击按钮时,我的函数不断返回错误。它似乎甚至没有返回函数内部的错误。我检查了我的UUID是否正确。这是CLI日志:

 Successfully transferred services-page.js.
Refreshing application...
Successfully synced application org.nativescript.myBLERegister on device FA72A03
04236.
JS: ---- Lollipop+ scanCallback result: {"type":"scanResult","UUID":"3A:01:3A:6A
:30:C6","name":null,"RSSI":-82,"state":"disconnected","advertisement":"Hv8GAAEJI
AImj9KgkqUJogD8bF6YsY/ZcTe3c0IL9gAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=","ma
nufacturerId":6,"manufacturerData":{}}
JS: ---- Lollipop+ scanCallback result: {"type":"scanResult","UUID":"EB:FA:DB:31
:1B:A7","name":"Adafruit Bluefruit LE","RSSI":-64,"state":"disconnected","advert
isement":"AgEGAgoAEQaeytwkDuWp4JPzo7UBAEBuFglBZGFmcnVpdCBCbHVlZnJ1aXQgTEUAAAAAAA
AAAAAAAAAAAAA="}
JS: ---- Lollipop+ scanCallback result: {"type":"scanResult","UUID":"30:AA:13:7D
:1F:4C","name":null,"RSSI":-86,"state":"disconnected","advertisement":"Hv8GAAEJI
AK0AyV4bcITNKXNBmQPDloOKoOyCtp+sAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=","ma
nufacturerId":6,"manufacturerData":{}}
JS: !!&&&&***** Clicked item with index 1
JS: --- peri selected: EB:FA:DB:31:1B:A7
JS: Connecting to peripheral with UUID: EB:FA:DB:31:1B:A7
JS: ------- _MyGattCallback.onConnectionStateChange, status: 0, new state: 2
JS: ---- discovering services..
JS: ------- _MyGattCallback.onServicesDiscovered, status (0=success): 0
JS: ------- Peripheral connected: {"UUID":"EB:FA:DB:31:1B:A7","name":"Adafruit B
luefruit LE","state":"connected","services":[{"UUID":"1800","characteristics":[{
"UUID":"2a00","name":"2a00","properties":{"read":true,"write":true,"writeWithout
Response":false,"notify":false,"indicate":false,"broadcast":false,"authenticated
SignedWrites":false,"extendedProperties":false},"descriptors":[]},{"UUID":"2a01"
,"name":"2a01","properties":{"read":true,"write":false,"writeWithoutResponse":fa
lse,"notify":false,"indicate":false,"broadcast":false,"authenticatedSignedWrites
":false,"extendedProperties":false},"descriptors":[]},{"UUID":"2a04","name":"2a0
4","properties":{"read":true,"write":false,"writeWithoutResponse":false,"notify"
:false,"indicate":false,"broadcast":false,"authenticatedSignedWrites":false,"ext
endedProperties":false},"descriptors":[]}]},{"UUID":"1801","characteristics":[{"
UUID":"2a05","name":"2a05","properties":{"read":false,"write":false,"writeWithou
tResponse":false,"notify":false,"indicate":true,"broadcast":false,"a...
JS: ---- ###### adding service: 1800
JS: ---- ###### adding service: 1801
JS: ---- ###### adding service: 00001530-1212-efde-1523-785feabcd123
JS: ---- ###### adding service: 180a
JS: ---- ###### adding service: 6e400001-b5a3-f393-e0a9-e50e24dcca9e
JS: Error in Bluetooth.writeWithoutResponse: TypeError: Cannot read property 'bl
uetoothGattService' of undefined
JS: Error in Bluetooth.writeWithoutResponse: TypeError: Cannot read property 'bl
uetoothGattService' of undefined
JS: Error in Bluetooth.writeWithoutResponse: TypeError: Cannot read property 'bl
uetoothGattService' of undefined

services.page.js

    function onWriteOpen() {
  bluetooth.writeWithoutResponse(
    {
      UUID: _peripheral.UUID,
      serviceUUID: '6e400001-b5a3-f393-e0a9-e50e24dcca9e', // read write service of BLE device
      characteristicUUID: '6e400002-b5a3-f393-e0a9-e50e24dcca9e', // transmit characteristic of BLE device service
      value: '0x4f',
      //awaitResponse: false // if false you will not get notified of errors (fire and forget) 
    }
   ).then(
    function(result) {
    console.log("Writing " + value + "on " + _peripheral.UUID);
    },
    function(err) {
      console.log("Failed to write " + value + "on "+ _peripheral.UUID + err);
    }
  );
}

带有按钮组件的xml文件

    <Page xmlns="http://www.nativescript.org/tns.xsd" loaded="pageLoaded">
  <StackLayout col="0" orientation="vertical">
    <StackLayout col="0" orientation="vertical" class="tab-content">
      <StackLayout orientation="horizontal">
        <Label text="{{ name }}" class="details-label-title" />
        <Label text=" services" class="details-label-title details-label-title-postfix" />
      </StackLayout>
      <Label text="{{ UUID }}" class="details-services-uuid" />
      <!--<Label text="{{ state }}" />-->
      <Button text="disconnect" tap="onDisconnectTap" class="button button-positive" />
    </StackLayout>

    <ActivityIndicator busy="{{ isLoading }}"/>

    <StackLayout>
    <Button text="Open" tap="onWriteOpen" class="button button-positive" />
    </StackLayout>

    <GridLayout rows="*">
      <ListView items="{{ services }}" itemTap="onServiceTap" separatorColor="#90c3d4">
        <ListView.itemTemplate>
          <StackLayout col="0" class="tab-content">
            <Label text="{{ name }}" />
            <Label text="{{ UUID }}" visibility="{{ name != UUID ? 'visible' : 'collapse' }}" />
          </StackLayout>
        </ListView.itemTemplate>
      </ListView>
    </GridLayout>

  </StackLayout>
</Page>

更新10/16: 使用Android 9 OS在Pixel 1上进行测试。 蓝牙插件中的插件AndroidManifest文件已经包含建议的权限行。

扫描时显示设备。这是该应用程序的两个屏幕截图: https://imgur.com/a/KLQS1mD

当我选择设备时,便可以看到可用的服务。我想使用最后一个按钮。我已使用演示应用程序将十六进制值手动写入设备,并且它可以正常工作。我只是想将整个操作附加到按钮上。 原始演示应用:https://github.com/EddyVerbruggen/nativescript-bluetooth-demo

10/17/18: 运行演示并手动选择写/读服务,然后写特征发送一个值,我得到这些结果。

JS: --- peri selected: EB:FA:DB:31:1B:A7
JS: Connecting to peripheral with UUID: EB:FA:DB:31:1B:A7
JS: ------- _MyGattCallback.onConnectionStateChange, status: 0, new state: 2
JS: ---- discovering services..
JS: ------- _MyGattCallback.onServicesDiscovered, status (0=success): 0
JS: ------- Peripheral connected: {"UUID":"EB:FA:DB:31:1B:A7","name":"ZoneFirst BLE","state":"connected","services":[{"UUID":"1800","characteristics":[{"UUID":"2a00","name":"2a00",
"properties":{"read":true,"write":true,"writeWithoutResponse":false,"notify":false,"indicate":false,"broadcast":false,"authenticatedSignedWrites":false,"extendedProperties":false},
"descriptors":[]},{"UUID":"2a01","name":"2a01","properties":{"read":true,"write":false,"writeWithoutResponse":false,"notify":false,"indicate":false,"broadcast":false,"authenticated
SignedWrites":false,"extendedProperties":false},"descriptors":[]},{"UUID":"2a04","name":"2a04","properties":{"read":true,"write":false,"writeWithoutResponse":false,"notify":false,"
indicate":false,"broadcast":false,"authenticatedSignedWrites":false,"extendedProperties":false},"descriptors":[]}]},{"UUID":"1801","characteristics":[{"UUID":"2a05","name":"2a05","
properties":{"read":false,"write":false,"writeWithoutResponse":false,"notify":false,"indicate":true,"broadcast":false,"authenticatedSignedWrites":false,"extendedProperties":false},
"descriptors":[{"UUID":"2902","value":null}]}]},{"UUID":"00001530-1212-efde-1523-785feabcd123","characteristics":[{"UUID":"00001532-1212-efde-1523-785feabcd123","name":"00001532-12
12-efde-1523-785feabcd123","properties":{"read":false,"write":false,"writeWithoutResponse":true,"notify":false,"indicate":false,"broadcast":false,"authenticatedSignedWrites":false,
"extendedProperties":false},"descriptors":[]},{"UUID":"00001531-1212-efde-1523-785feabcd123","name":"00001531-1212-efde-1523-785feabcd123","properties":{"read":false,"write":true,"
writeWithoutResponse":false,"notify":true,"indicate":false,"broadcast":false,"authenticatedSignedWrites":false,"extendedProperties":false},"descriptors":[{"UUID":"2902","value":nul
l}]},{"UUID":"00001534-1212-efde-1523-785feabcd123","name":"00001534-1212-efde-1523-785feabcd123","properties":{"read":true,"write":false,"writeWithoutResponse":false,"notify":fals
e,"indicate":false,"broadcast":false,"authenticatedSignedWrites":false,"extendedProperties":false},"descriptors":[]}]},{"UUID":"180a","characteristics":[{"UUID":"2a29","name":"2a29
","properties":{"read":true,"write":false,"writeWithoutResponse":false,"notify":false,"indicate":false,"broadcast":false,"authenticatedSignedWrites":false,"extendedProperties":fals
e},"descriptors":[]},{"UUID":"2a24","name":"2a24","properties":{"read":true,"write":false,"writeWithoutResponse":false,"notify":false,"indicate":false,"broadcast":false,"authentica
tedSignedWrites":false,"extendedProperties":false},"descriptors":[]},{"UUID":"2a28","name":"2a28","properties":{"read":true,"write":false,"writeWithoutResponse":false,"notify":fals
e,"indicate":false,"broadcast":false,"authenticatedSignedWrites":false,"extendedProperties":false},"descriptors":[]},{"UUID":"2a26","name":"2a26","properties":{"read":true,"write":
false,"writeWithoutResponse":false,"notify":false,"indicate":false,"broadcast":false,"authenticatedSignedWrites":false,"extendedProperties":false},"descriptors":[]},{"UUID":"2a27",
"name":"2a27","properties":{"read":true,"write":false,"writeWithoutResponse":false,"notify":false,"indicate":false,"broadcast":false,"authenticatedSignedWrites":false,"extendedProp
erties":false},"descriptors":[]}]},{"UUID":"6e400001-b5a3-f393-e0a9-e50e24dcca9e","characteristics":[{"UUID":"6e400003-b5a3-f393-e0a9-e50e24dcca9e","name":"6e400003-b5a3-f393-e0a9-
e50e24dcca9e","properties":{"read":false,"write":false,"writeWithoutResponse":false,"notify":true,"indicate":false,"broadcast":false,"authenticatedSignedWrites":false,"extendedProp
erties":false},"descriptors":[{"UUID":"2901","value":null},{"UUID":"2902","value":null}]},{"UUID":"6e400002-b5a3-f393-e0a9-e50e24dcca9e","name":"6e400002-b5a3-f393-e0a9-e50e24dcca9
e","properties":{"read":false,"write":true,"writeWithoutResponse":true,"notify":false,"indicate":false,"broadcast":false,"authenticatedSignedWrites":false,"extendedProperties":fals
e},"descriptors":[{"UUID":"2901","value":nul
JS: l}]}]}]}
JS: ---- ###### adding service: 1800
JS: ---- ###### adding service: 1801
JS: ---- ###### adding service: 00001530-1212-efde-1523-785feabcd123
JS: ---- ###### adding service: 180a
JS: ---- ###### adding service: 6e400001-b5a3-f393-e0a9-e50e24dcca9e
JS: --- service selected: 6e400001-b5a3-f393-e0a9-e50e24dcca9e
JS: ------- _MyGattCallback.onCharacteristicWrite
JS: android.bluetooth.BluetoothGattCharacteristic@75d8936

在这种情况下,我可以看到写入的回调。但是使用按钮会返回 TypeError:无法读取未定义的属性'bluetoothGattService'

0 个答案:

没有答案