Я работаю над приложением Bluetooth 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: тестирование наПиксель 1 с ОС Android 9.Файл плагина AndroidManifest в плагине bluetooth уже содержит строку с предложенными разрешениями.
Устройство отображается при сканировании.Вот два скриншота приложения: 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 с неопределенным значением