Skip to content

Commit c88e5f0

Browse files
committed
docs: update readme
1 parent ebb7c38 commit c88e5f0

File tree

1 file changed

+86
-44
lines changed

1 file changed

+86
-44
lines changed

README.md

Lines changed: 86 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
`npm i -S @manekinekko/angular-web-bluetooth @types/web-bluetooth`
1414

15-
Note: Make also sure the `@types/web-bluetooth` is installed OK.
15+
_Note: Make also sure the `@types/web-bluetooth` is installed correctly in your `node_modules`. _
1616

1717
## Use it
1818

@@ -26,15 +26,61 @@ import { WebBluetoothModule } from '@manekinekko/angular-web-bluetooth';
2626
imports: [
2727
//...,
2828
WebBluetoothModule.forRoot({
29-
enableTracing: true / false // enable logs
29+
enableTracing: true // or false, this will enable logs in the browser's console
3030
})
3131
]
3232
//...
3333
})
3434
export class AppModule {}
3535
```
3636

37-
## 2) use it in your service/component
37+
## 2.a) use it in your service/component (the easiest way)
38+
39+
Here is an annotated example using the `BluetoothCore` service:
40+
41+
```javascript
42+
import { Injectable } from '@angular/core';
43+
import { BluetoothCore } from '@manekinekko/angular-web-bluetooth';
44+
45+
@Injectable({
46+
providedIn: 'root'
47+
})
48+
export class BatteryLevelService {
49+
50+
constructor(public readonly ble: BluetoothCore) {}
51+
52+
getDevice() {
53+
// call this method to get the connected device
54+
return this.ble.getDevice$();
55+
}
56+
57+
stream() {
58+
// call this method to get a stream of values emitted by the device for a given characteristic
59+
return this.ble.streamValues$().pipe(
60+
map((value: DataView) => value.getInt8(0))
61+
);
62+
}
63+
64+
disconnectDevice() {
65+
// call this method to disconnect from the device. This method will also stop clear all subscribed notifications
66+
this.ble.disconnectDevice();
67+
}
68+
69+
value() {
70+
console.log('Getting Battery level...');
71+
72+
return this.ble
73+
.value$({
74+
service: 'battery_service',
75+
characteristic: 'battery_level'
76+
});
77+
}
78+
79+
}
80+
```
81+
82+
83+
## 2.b) use it in your service/component (the advanced way)
3884

3985
Here is an annotated example using the `BluetoothCore` service:
4086

@@ -52,68 +98,64 @@ export class BatteryLevelService {
5298

5399
constructor(public ble: BluetoothCore) {}
54100

55-
getFakeValue() {
56-
this.ble.fakeNext();
57-
}
58-
59101
getDevice() {
60102
// call this method to get the connected device
61103
return this.ble.getDevice$();
62104
}
63105

64-
streamValues() {
106+
stream() {
65107
// call this method to get a stream of values emitted by the device
66108
return this.ble.streamValues$().pipe(map((value: DataView) => value.getUint8(0)));
67109
}
68110

111+
disconnectDevice() {
112+
this.ble.disconnectDevice();
113+
}
114+
69115
/**
70116
* Get Battery Level GATT Characteristic value.
71117
* This logic is specific to this service, this is why we can't abstract it elsewhere.
72-
* The developer is free to provide any service, and characteristics she wants.
118+
* The developer is free to provide any service, and characteristics they want.
73119
*
74120
* @return Emites the value of the requested service read from the device
75121
*/
76-
getBatteryLevel() {
77-
console.log('Getting Battery Service...');
78-
79-
try {
80-
return (
81-
this.ble
82-
83-
// 1) call the discover method will trigger the discovery process (by the browser)
84-
.discover$({
85-
acceptAllDevices: true,
86-
optionalServices: [BatteryLevelService.GATT_PRIMARY_SERVICE]
87-
})
88-
.pipe(
89-
// 2) get that service
90-
mergeMap((gatt: BluetoothRemoteGATTServer) => {
91-
return this.ble.getPrimaryService$(gatt, BatteryLevelService.GATT_PRIMARY_SERVICE);
92-
}),
93-
// 3) get a specific characteristic on that service
94-
mergeMap((primaryService: BluetoothRemoteGATTService) => {
95-
return this.ble.getCharacteristic$(primaryService, BatteryLevelService.GATT_CHARACTERISTIC_BATTERY_LEVEL);
96-
}),
97-
// 4) ask for the value of that characteristic (will return a DataView)
98-
mergeMap((characteristic: BluetoothRemoteGATTCharacteristic) => {
99-
return this.ble.readValue$(characteristic);
100-
}),
101-
// 5) on that DataView, get the right value
102-
map((value: DataView) => value.getUint8(0))
103-
)
104-
);
105-
} catch (e) {
106-
console.error('Oops! can not read value from %s');
107-
}
122+
value() {
123+
console.log('Getting Battery level...');
124+
125+
return this.ble
126+
127+
// 1) call the discover method will trigger the discovery process (by the browser)
128+
.discover$({
129+
acceptAllDevices: true,
130+
optionalServices: [BatteryLevelService.GATT_PRIMARY_SERVICE]
131+
})
132+
.pipe(
133+
134+
// 2) get that service
135+
mergeMap((gatt: BluetoothRemoteGATTServer) => {
136+
return this.ble.getPrimaryService$(gatt, BatteryLevelService.GATT_PRIMARY_SERVICE);
137+
}),
138+
139+
// 3) get a specific characteristic on that service
140+
mergeMap((primaryService: BluetoothRemoteGATTService) => {
141+
return this.ble.getCharacteristic$(primaryService, BatteryLevelService.GATT_CHARACTERISTIC_BATTERY_LEVEL);
142+
}),
143+
144+
// 4) ask for the value of that characteristic (will return a DataView)
145+
mergeMap((characteristic: BluetoothRemoteGATTCharacteristic) => {
146+
return this.ble.readValue$(characteristic);
147+
}),
148+
149+
// 5) on that DataView, get the right value
150+
map((value: DataView) => value.getUint8(0))
151+
)
108152
}
109153
}
110154
```
111155

112-
See the [starter](https://github.com/manekinekko/angular-web-bluetooth-starter/tree/master/src/app) for a complete use case.
113-
114156
## API documentation
115157

116-
Here 👉 https://manekinekko.github.io/angular-web-bluetooth/
158+
Here 👉 https://manekinekko.github.io/angular-web-bluetooth/
117159

118160
## Need a starter?
119161

0 commit comments

Comments
 (0)