12
12
13
13
` npm i -S @manekinekko/angular-web-bluetooth @types/web-bluetooth `
14
14
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 ` . _
16
16
17
17
## Use it
18
18
@@ -26,15 +26,61 @@ import { WebBluetoothModule } from '@manekinekko/angular-web-bluetooth';
26
26
imports: [
27
27
// ...,
28
28
WebBluetoothModule .forRoot ({
29
- enableTracing: true / false // enable logs
29
+ enableTracing: true // or false, this will enable logs in the browser's console
30
30
})
31
31
]
32
32
// ...
33
33
})
34
34
export class AppModule {}
35
35
```
36
36
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)
38
84
39
85
Here is an annotated example using the ` BluetoothCore ` service:
40
86
@@ -52,68 +98,64 @@ export class BatteryLevelService {
52
98
53
99
constructor (public ble : BluetoothCore ) {}
54
100
55
- getFakeValue () {
56
- this .ble .fakeNext ();
57
- }
58
-
59
101
getDevice () {
60
102
// call this method to get the connected device
61
103
return this .ble .getDevice$ ();
62
104
}
63
105
64
- streamValues () {
106
+ stream () {
65
107
// call this method to get a stream of values emitted by the device
66
108
return this .ble .streamValues$ ().pipe (map ((value : DataView ) => value .getUint8 (0 )));
67
109
}
68
110
111
+ disconnectDevice () {
112
+ this .ble .disconnectDevice ();
113
+ }
114
+
69
115
/**
70
116
* Get Battery Level GATT Characteristic value.
71
117
* 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 .
73
119
*
74
120
* @return Emites the value of the requested service read from the device
75
121
*/
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
+ )
108
152
}
109
153
}
110
154
```
111
155
112
- See the [ starter] ( https://github.com/manekinekko/angular-web-bluetooth-starter/tree/master/src/app ) for a complete use case.
113
-
114
156
## API documentation
115
157
116
- Here 👉 https://manekinekko.github.io/angular-web-bluetooth/
158
+ Here 👉 https://manekinekko.github.io/angular-web-bluetooth/
117
159
118
160
## Need a starter?
119
161
0 commit comments