Skip to content

Commit 0e37fdf

Browse files
authored
Merge pull request #689 from Xnaff/unicode-encode-decode
Add helper function to encode and decode unicode strings
2 parents ca2802d + 10fa0d7 commit 0e37fdf

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

readme.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ This plugin allows you to interact with Bluetooth LE devices on Android, iOS, an
8282
- [bytesToEncodedString](#bytestoencodedstring)
8383
- [stringToBytes](#stringtobytes)
8484
- [bytesToString](#bytestostring)
85+
- [encodeUnicode](#encodeunicode)
86+
- [decodeUnicode](#decodeunicode)
8587
- [Example](#example)
8688
- [Data Parsing Example](#data-parsing-example)
8789
- [Sample: Discover and interact with Bluetooth LE devices](#sample-discover-and-interact-with-bluetooth-le-devices)
@@ -254,6 +256,8 @@ Neither Android nor iOS support Bluetooth on emulators, so you'll need to test o
254256
* [bluetoothle.bytesToEncodedString](#bytestoencodedstring)
255257
* [bluetoothle.stringToBytes](#stringtobytes)
256258
* [bluetoothle.bytesToString](#bytestostring)
259+
* [bluetoothle.encodeUnicode](#encodeunicode)
260+
* [bluetoothle.decodeUnicode](#decodeunicode)
257261

258262

259263

@@ -1271,6 +1275,8 @@ To write without response, set type to "noResponse". Any other value will defaul
12711275
var string = "Write Hello World";
12721276
var bytes = bluetoothle.stringToBytes(string);
12731277
var encodedString = bluetoothle.bytesToEncodedString(bytes);
1278+
// if your code includes special characters you should use the encodeUnicode helper function
1279+
var encodedUnicodeString = bluetoothle.encodeUnicode(string);
12741280

12751281
//Note, this example doesn't actually work since it's read only characteristic
12761282
{"value":"V3JpdGUgSGVsbG8gV29ybGQ=","service":"180F","characteristic":"2A19","type":"noResponse","address":"ABC123"}
@@ -1283,6 +1289,9 @@ Value is a base64 encoded string of written bytes. Use bluetoothle.encodedString
12831289
var returnObj = {"status":"written","service":"180F","characteristic":"2A19","value":"V3JpdGUgSGVsbG8gV29ybGQ=","address":"ABC123"}
12841290
var bytes = bluetoothle.encodedStringToBytes(returnObj.value);
12851291
var string = bluetoothle.bytesToString(bytes); //This should equal Write Hello World
1292+
1293+
// if your code includes special characters you should use the decodeUnicode helper function
1294+
var string = bluetoothle.decodeUnicode(returnObj.value);
12861295
```
12871296

12881297

@@ -2243,6 +2252,20 @@ if (obj.status == "subscribedResult")
22432252
}
22442253
```
22452254

2255+
### encodeUnicode ###
2256+
Helper function to convert unicode string to base64 encoded string. This function can be used to encode special characters such as emojis.
2257+
2258+
```javascript
2259+
bluetoothle.encodeUnicode(string);
2260+
```
2261+
2262+
### decodeUnicode ###
2263+
Helper function to convert a base64 encoded string to unicode string. This function also decodes special characters such as emojis.
2264+
2265+
```javascript
2266+
bluetoothle.decodeUnicode(string);
2267+
```
2268+
22462269
## Sample: Discover and interact with Bluetooth LE devices ##
22472270

22482271
We'll build an app that lets you discover Bluetooth Low Energy (LE) devices that are around you, connect to a one, and then look at all of the information that you can obtain from that device such as signal strength, supported services, battery level and more.

types/index.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,20 @@ declare namespace BluetoothlePlugin {
592592
* @return Encoded string
593593
*/
594594
bytesToString(value: Uint8Array): string;
595+
596+
/**
597+
* Helper function to convert string to base64.
598+
* @param value string
599+
* @return base64 string
600+
*/
601+
encodeUnicode(value: string): string;
602+
603+
/**
604+
* Helper function to convert bytes to a string.
605+
* @param value base64 string
606+
* @return string
607+
*/
608+
decodeUnicode(value: string): string;
595609
}
596610

597611
/* Available status of device */

www/bluetoothle.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,17 @@ var bluetoothle = {
170170
bytesToString: function(bytes) {
171171
return String.fromCharCode.apply(null, new Uint16Array(bytes));
172172
},
173+
encodeUnicode: function(str) {
174+
return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, (match, p1) => {
175+
return String.fromCharCode(parseInt(p1, 16))
176+
}))
177+
},
178+
decodeUnicode: function(str) {
179+
// Going backwards: from byte stream, to percent-encoding, to original string.
180+
return decodeURIComponent(atob(str).split('').map((c) => {
181+
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
182+
}).join(''));
183+
},
173184
bytesToHex: function(bytes) {
174185
var string = [];
175186
for (var i = 0; i < bytes.length; i++) {

0 commit comments

Comments
 (0)