Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Blueberry ring #257

Merged
merged 6 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 24 additions & 22 deletions assets/js/bluetooth/bluetooth.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class BluetoothDevice extends EventTarget {

async watchAdvertisements() {
console.log("BluetoothRemoteGATTServer:watchAdvertisements ");
const response = await window.axs.callHandler(
const response = await window.axs?.callHandlerWrapper(
"BluetoothDevice.watchAdvertisements"
);
console.log(
Expand All @@ -45,7 +45,7 @@ class BluetoothDevice extends EventTarget {

async forget() {
console.log("BluetoothRemoteGATTServer:forget ");
const response = await window.axs.callHandler("BluetoothDevice.forget");
const response = await window.axs?.callHandlerWrapper("BluetoothDevice.forget");
console.log(
"BluetoothRemoteGATTServer:forget ",
JSON.stringify(response, null, 2)
Expand All @@ -63,7 +63,7 @@ class BluetoothRemoteGATTServer extends EventTarget {

async connect() {
console.log("BluetoothRemoteGATTServer:connect ");
const response = await window.axs?.callHandler(
const response = await window.axs?.callHandlerWrapper(
"BluetoothRemoteGATTServer.connect",
{}
);
Expand All @@ -78,13 +78,13 @@ class BluetoothRemoteGATTServer extends EventTarget {

async disconnect() {
console.log("BluetoothRemoteGATTServer:disconnect ");
await window.axs.callHandler("BluetoothRemoteGATTServer.disconnect", {});
await window.axs?.callHandlerWrapper("BluetoothRemoteGATTServer.disconnect", {});
}

async getPrimaryService(service) {
console.log("BluetoothRemoteGATTServer:getPrimaryService ", service);
const data = { service: service };
const response = await window.axs?.callHandler(
const response = await window.axs?.callHandlerWrapper(
"BluetoothRemoteGATTServer.getPrimaryService",
data
);
Expand All @@ -104,7 +104,7 @@ class BluetoothRemoteGATTServer extends EventTarget {
async getPrimaryServices(service) {
console.log("BluetoothRemoteGATTServer:getPrimaryServices ", service);
const data = { service: service };
const response = await window.axs.callHandler(
const response = await window.axs?.callHandlerWrapper(
"BluetoothRemoteGATTServer.getPrimaryServices",
data
);
Expand All @@ -128,7 +128,7 @@ class BluetoothRemoteGATTCharacteristic extends EventTarget {
serviceUUID: this.service.uuid,
descriptor: descriptor,
};
const response = await window.axs.callHandler(
const response = await window.axs?.callHandlerWrapper(
"BluetoothRemoteGATTCharacteristic.getDescriptor",
data
);
Expand All @@ -146,7 +146,7 @@ class BluetoothRemoteGATTCharacteristic extends EventTarget {
descriptor: descriptor,
};

const response = await window.axs.callHandler(
const response = await window.axs?.callHandlerWrapper(
"BluetoothRemoteGATTCharacteristic.getDescriptors",
data
);
Expand All @@ -156,7 +156,7 @@ class BluetoothRemoteGATTCharacteristic extends EventTarget {
async readValue() {
console.log("BluetoothRemoteGATTCharacteristic:readValue");
const data = { this: this.uuid, serviceUUID: this.service.uuid };
const response = await window.axs.callHandler(
const response = await window.axs?.callHandlerWrapper(
"BluetoothRemoteGATTCharacteristic.readValue",
data
);
Expand All @@ -182,7 +182,7 @@ class BluetoothRemoteGATTCharacteristic extends EventTarget {
value: value,
};

const response = await window.axs.callHandler(
const response = await window.axs?.callHandlerWrapper(
"BluetoothRemoteGATTCharacteristic.writeValue",
data
);
Expand All @@ -192,9 +192,7 @@ class BluetoothRemoteGATTCharacteristic extends EventTarget {
JSON.stringify(response, null, 2)
);

if (response.error !== undefined && response.error === true) {
throw new Error("Error while writing value.");
}
return {};
}

async writeValueWithResponse(value) {
Expand All @@ -208,10 +206,12 @@ class BluetoothRemoteGATTCharacteristic extends EventTarget {
value: value,
};

await window.axs.callHandler(
await window.axs?.callHandlerWrapper(
"BluetoothRemoteGATTCharacteristic.writeValueWithResponse",
data
);

return {};
}

async writeValueWithoutResponse(value) {
Expand All @@ -225,17 +225,19 @@ class BluetoothRemoteGATTCharacteristic extends EventTarget {
value: value,
};

await window.axs.callHandler(
await window.axs?.callHandlerWrapper(
"BluetoothRemoteGATTCharacteristic.writeValueWithoutResponse",
data
);

return {};
}

async startNotifications() {
console.log("BluetoothRemoteGATTCharacteristic:startNotifications");
const data = { this: this.uuid, serviceUUID: this.service.uuid };

await window.axs.callHandler(
await window.axs?.callHandlerWrapper(
"BluetoothRemoteGATTCharacteristic.startNotifications",
data
);
Expand All @@ -246,7 +248,7 @@ class BluetoothRemoteGATTCharacteristic extends EventTarget {
console.log("BluetoothRemoteGATTCharacteristic:stopNotifications");
const data = { this: this.uuid, serviceUUID: this.service.uuid };

await window.axs.callHandler(
await window.axs?.callHandlerWrapper(
"BluetoothRemoteGATTCharacteristic.stopNotifications",
data
);
Expand All @@ -273,7 +275,7 @@ class BluetoothRemoteGATTService extends EventTarget {
characteristic
);
let data = { this: this.uuid, characteristic: characteristic };
const response = await window.axs?.callHandler(
const response = await window.axs?.callHandlerWrapper(
"BluetoothRemoteGATTService.getCharacteristic",
data
);
Expand All @@ -296,7 +298,7 @@ class BluetoothRemoteGATTService extends EventTarget {
"BluetoothRemoteGATTService:getCharacteristics ",
characteristic
);
const response = await window.axs.callHandler(
const response = await window.axs?.callHandlerWrapper(
"BluetoothRemoteGATTService.getCharacteristics",
{ this: "$uuid", characteristic: characteristic }
);
Expand All @@ -305,7 +307,7 @@ class BluetoothRemoteGATTService extends EventTarget {

async getIncludedService(service) {
console.log("BluetoothRemoteGATTService:getIncludedService ", service);
const response = await window.axs.callHandler(
const response = await window.axs?.callHandlerWrapper(
"BluetoothRemoteGATTService.getIncludedService",
{ this: "$uuid", service: service }
);
Expand All @@ -314,7 +316,7 @@ class BluetoothRemoteGATTService extends EventTarget {

async getIncludedServices(service) {
console.log("BluetoothRemoteGATTService:getIncludedServices ", service);
const response = await window.axs.callHandler(
const response = await window.axs?.callHandlerWrapper(
"BluetoothRemoteGATTService.getIncludedServices",
{ this: "$uuid", service: service }
);
Expand All @@ -336,7 +338,7 @@ class AXSBluetooth {

async requestDevice(options) {
console.log("AXSBluetooth:requestDevice ", options);
const response = await window.axs?.callHandler("requestDevice", options);
const response = await window.axs?.callHandlerWrapper("requestDevice", options);

const gatt = new BluetoothRemoteGATTServer(
response.gatt.device,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ class BluetoothUseCase extends ReactiveUseCase {
final ChainConfigurationUseCase _chainConfigurationUseCase;
final AuthUseCase _authUseCase;

late StreamSubscription<List<ScanResult>>? scannerListener;
late StreamSubscription<BluetoothAdapterState>? stateListener;
late StreamSubscription<bool>? isScanningStateListener;
StreamSubscription<List<ScanResult>>? scannerListener;
StreamSubscription<BluetoothAdapterState>? stateListener;
StreamSubscription<bool>? isScanningStateListener;

late final ValueStream<bool> isScanning = reactive(false);
late final ValueStream<BluetoothAdapterState> bluetoothStatus =
Expand All @@ -49,8 +49,13 @@ class BluetoothUseCase extends ReactiveUseCase {
// usually start scanning, connecting, etc
isScanningListener();
initScannerListener();
} else {
} else if (state == BluetoothAdapterState.unauthorized ||
state == BluetoothAdapterState.unavailable ||
state == BluetoothAdapterState.off ||
state == BluetoothAdapterState.unknown) {
// show an error to the user, etc
cancelIsScanningListener();
_cancelScannerListen();
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ class BlueberryResolves {
}

int parseDate(List<int> data) {
final parts = BlueberryCommandsUtils.radix16bcd(data);
// 200x24-0x06-0x05 0x06:0x39:0x02
final parts = BlueberryCommandsUtils.radix16bcd(data, no0x: true);
final ymd = '20${parts[0]}-${parts[1]}-${parts[2]}';
final hms = '${parts[3]}:${parts[4]}:${parts[5]}';
final date = DateTime.parse('$ymd $hms');
Expand Down
37 changes: 19 additions & 18 deletions lib/features/dapps/subfeatures/open_dapp/open_dapp_presenter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,15 @@ class OpenDAppPresenter extends CompletePresenter<OpenDAppState> {
@override
Future<void> dispose() {
characteriticListnerTimer?.cancel();
closeBlueberryConnection();
return super.dispose();
}

// Disconnects from Blueberry If there a selected device.
void closeBlueberryConnection() {
state.selectedScanResult?.device.disconnect();
}

void onWebViewCreated(InAppWebViewController controller) async {
notify(() => state.webviewController = controller);
updateCurrentUrl(null);
Expand Down Expand Up @@ -809,7 +815,8 @@ class OpenDAppPresenter extends CompletePresenter<OpenDAppState> {
Future<Map<String, dynamic>>
handleBluetoothRemoteGATTCharacteristicStartNotifications(
Map<String, dynamic> data) async {
collectLog('handleBluetoothRemoteGATTCharacteristicStartNotifications : $data');
collectLog(
'handleBluetoothRemoteGATTCharacteristicStartNotifications : $data');
final selectedService = await getSelectedService(data['serviceUUID']);
final selectedCharacteristic =
getSelectedCharacteristic(data['this'], selectedService);
Expand All @@ -826,7 +833,8 @@ class OpenDAppPresenter extends CompletePresenter<OpenDAppState> {
Future<Map<String, dynamic>>
handleBluetoothRemoteGATTCharacteristicStopNotifications(
Map<String, dynamic> data) async {
collectLog('handleBluetoothRemoteGATTCharacteristicStopNotifications : $data');
collectLog(
'handleBluetoothRemoteGATTCharacteristicStopNotifications : $data');
final selectedService = await getSelectedService(data['serviceUUID']);
final selectedCharacteristic =
getSelectedCharacteristic(data['this'], selectedService);
Expand All @@ -846,19 +854,16 @@ class OpenDAppPresenter extends CompletePresenter<OpenDAppState> {
final selectedService = await getSelectedService(data['serviceUUID']);
final selectedCharacteristic =
getSelectedCharacteristic(data['this'], selectedService);
final value = Uint8List.fromList(List<int>.from((data['value'] as Map<String, dynamic>).values.toList()));
final value = Uint8List.fromList(List<int>.from(
(data['value'] as Map<String, dynamic>).values.toList()));

try {
collectLog('handleWrites:value $value');
if (withResponse) {
await selectedCharacteristic.write(value);
} else {
await selectedCharacteristic.write(value, withoutResponse: true);
}
return {};
} catch (e) {
return {'error': 'true'};
collectLog('handleWrites:value $value');
if (withResponse) {
await selectedCharacteristic.write(value);
} else {
await selectedCharacteristic.write(value, withoutResponse: true);
}
return {};
}

Future<Map<String, dynamic>>
Expand Down Expand Up @@ -903,14 +908,10 @@ class OpenDAppPresenter extends CompletePresenter<OpenDAppState> {
blue_plus.BluetoothCharacteristic characteristic,
) async {
await characteristic.setNotifyValue(true);
// characteriticListnerTimer = Timer.periodic(const Duration(seconds: 5), (timer) {
// characteristic.read();
// });

characteristicValueStreamSubscription =
characteristic.lastValueStream.listen((event) async {
final uInt8List = Uint8List.fromList(event);
print(uInt8List);
collectLog('characteristicValueStreamSubscription:event $event');
collectLog(
'characteristicValueStreamSubscription:uInt8List ${uInt8List.toString()}');
Expand Down Expand Up @@ -1006,7 +1007,7 @@ class OpenDAppPresenter extends CompletePresenter<OpenDAppState> {
status: AXSJSChannelResponseStatus.failed,
data: null,
message: e.toString());
return response.toMap((data) => {'message': e.toString()});
return response.toMap((data) => {'message': e});
}
}

Expand Down
Loading
Loading