支持:iOS和Android4.3+
安裝:Cordova
$ cordova plugin add cordova-plugin-ble-central https://github.com/don/cordova-plugin-ble-central#phonegap
PhoneGap
$ phonegap plugin add cordova-plugin-ble-central https://github.com/don/cordova-plugin-ble-central#phonegap-build
API:
scan 掃描:
藍牙掃描的方法,第二個參數(shù)10指的掃描時間,單位是秒,device是掃描的設(shè)備
ble.scan(services, seconds, success, failure);
參數(shù):
- services: 發(fā)現(xiàn)的服務(wù)列表或[ ]中所有設(shè)備
- seconds: 時間
- success: 成功回調(diào)
- failure: 失敗回調(diào)
example:
ble.scan([], 5, function(device) {
console.log(JSON.stringify(device));
}, failure);
startScan 開始掃描:
ble.startScan(services, success, failure);
參數(shù):
- services: 發(fā)現(xiàn)的服務(wù)列表或[]中所有設(shè)備
- success: 成功回調(diào)
- failure: 失敗回調(diào)
example:
ble.startScan([], function(device) {
console.log(JSON.stringify(device));
}, failure);
setTimeout(ble.stopScan,
5000,
function() { console.log("Scan complete"); },
function() { console.log("stopScan failed"); }
);
startScanWithOptions 指定掃描:
ble.startScanWithOptions(services, options, success, failure);
參數(shù):
- services: 發(fā)現(xiàn)的服務(wù)列表或[]中所有設(shè)備
- options: 一個對象指定一組鍵值對,現(xiàn)在支持的選項如下:
true:如果復(fù)制設(shè)備應(yīng)該報道,
false:(默認)如果設(shè)備只報告一次。(可選) - success: 成功回調(diào)
- failure: 失敗回調(diào)
example:
ble.startScan([],
{ reportDuplicates: true }
function(device) {
console.log(JSON.stringify(device));
},
failure);
setTimeout(ble.stopScan,
5000,
function() { console.log("Scan complete"); },
function() { console.log("stopScan failed"); }
);
stopScan 停止掃描:
ble.stopScan(success, failure);
參數(shù):
- success: 成功回調(diào)
- failure: 失敗回調(diào)
example:
ble.startScan([], function(device) {
console.log(JSON.stringify(device));
}, failure);
setTimeout(ble.stopScan,
5000,
function() { console.log("Scan complete"); },
function() { console.log("stopScan failed"); }
);
/* Alternate syntax
setTimeout(function() {
ble.stopScan(
function() { console.log("Scan complete"); },
function() { console.log("stopScan failed"); }
);
}, 5000);
*/
connect 連接:
連接藍牙的方法盆驹,第一個參數(shù)是你掃描到的設(shè)備的id,后面的是成功和失敗的回調(diào)
ble.connect(device_id, connectSuccess, connectFailure);
參數(shù):
- device_id: uuid或者設(shè)備的mac地址
- connectSuccess: 連接成功回調(diào)
- connectFailure: 連接失敗回調(diào)
example:
$scope.connectFun=function(device){
ble.connect(device.id, $scope.onConnected, $scope.onError);
}
disconnect 斷開連接:
ble.disconnect(device_id, [success], [failure]);
參數(shù):
- device_id: UUID 或者設(shè)備 MAC 地址
- success: 成功回調(diào)
- failure: 失敗回調(diào)
example:
ble.disconnect(device.id, function(){
//do something
}, function(){
//do something
});
read 讀:
ble.read(device_id, service_uuid, characteristic_uuid, success, failure);
參數(shù):
- device_id: 設(shè)備UUID 或者 設(shè)備 MAC 地址
- service_uuid: 藍牙設(shè)備的uuid
- characteristic_uuid: UUID of the BLE characteristic
- success: Success callback function that is invoked when the connection is successful. [optional]
- failure: Error callback function, invoked when error occurs. [optional]
example:
readCounter = setInterval(function () {
ble.read(device.id, $scope.serviceUUID, $scope.counterCharacteristic,
$scope.onDataReader, $scope.onReadError);}, 1000);
$scope.serviceUUID:藍牙的UUID衰琐,具體不懂宏娄,每個藍牙都有毙石,但是這個值需要注意的地方就是它在android和ios上的寫法不一樣凿蒜,比如在android上它的值是xxxxfff0-xxxx-xxxx-xxxx-xxxxxxxxxxx,那么它在ios上就是FFF0禁谦,這個可以用ionic.Platform.isAndroid()進行平臺判斷
$scope.counterCharacteristic:藍牙的特性值,寫法跟UUID類似废封,在android和ios的差異寫法也跟UUID一樣州泊,剛開始我在android上寫好功能后在iOS上連不上藍牙問題就出在了這里
$scope.onDataReader:成功的回調(diào),可以進行讀取數(shù)據(jù)
$scope.onReadError:失敗的回調(diào)
讀取數(shù)據(jù)擴充:
$scope.onDataReader=function(buffer){
//buffer就是藍牙讀取的數(shù)據(jù)漂洋,但是需要轉(zhuǎn)換才能被引用
var data = new Uint8Array(buffer);//Uint8Array對象:8 位無符號整數(shù)值的類型化數(shù)組遥皂。內(nèi)容將初始化為0。如果無法分配請求數(shù)目的字節(jié)刽漂,則將引發(fā)異常演训。
//這里可以一步步打印data然后按需要轉(zhuǎn)出所需的數(shù)據(jù)
//將值賦值給頁面上綁定的變量時,如果變量沒有變化贝咙,試著用
$scope.$apply(function(){
//將計算后的數(shù)據(jù)給變量賦值样悟,要用$apply涉及到了ng的臟值檢查機制,有興趣可以去搜搜相關(guān)資料
})
}
write 寫:
ble.write(device_id, service_uuid, characteristic_uuid, data, success, failure);
參數(shù):
- device_id: 設(shè)備藍牙的UUID或設(shè)備的mac地址
- service_uuid: 藍牙服務(wù)的UUID
- characteristic_uuid: 具有特征值的藍牙的UUID
- data: 利用數(shù)組存儲的二進制數(shù)
- success: 成功回調(diào)
- failure: 失敗回調(diào)
example:
// send 1 byte to switch a light on
var data = new Uint8Array(1);
data[0] = 1;
ble.write(device_id, "FF10", "FF11", data.buffer, success, failure);
// send a 3 byte value with RGB color
var data = new Uint8Array(3);
data[0] = 0xFF; // red
data[1] = 0x00; // green
data[2] = 0xFF; // blue
ble.write(device_id, "ccc0", "ccc1", data.buffer, success, failure);
// send a 32 bit integer
var data = new Uint32Array(1);
data[0] = counterInput.value;
ble.write(device_id, SERVICE, CHARACTERISTIC, data.buffer, success, failure);
writeWithoutResponse 無響應(yīng)的寫入:
向特征設(shè)備寫入無返回響應(yīng)的數(shù)據(jù)
ble.writeWithoutResponse(device_id, service_uuid, characteristic_uuid, data, success, failure);
參數(shù):
- device_id: 設(shè)備藍牙的UUID或設(shè)備的mac地址
- service_uuid: 藍牙服務(wù)的UUID
- characteristic_uuid: 具有特征值的藍牙的UUID
- data: 利用數(shù)組存儲的二進制數(shù)
- success: 成功回調(diào)
- failure: 失敗回調(diào)
startNotification 開始通知:
ble.startNotification(device_id, service_uuid, characteristic_uuid, success, failure);
參數(shù):
- device_id: 設(shè)備藍牙的UUID或設(shè)備的mac地址
- service_uuid: 藍牙服務(wù)的UUID
- characteristic_uuid: 具有特征值的藍牙的UUID
- data: 利用數(shù)組存儲的二進制數(shù)
- success: 成功回調(diào)
- failure: 失敗回調(diào)
ble.startNotification(device_id, "FFE0", "FFE1", onData, failure);
example:
var onData = function(buffer) {
// Decode the ArrayBuffer into a typed Array based on the data you expect
var data = new Uint8Array(buffer);
alert("Button state changed to " + data[0]);
}
stopNotification 停止通知:
ble.stopNotification(divece_id,servece_uuid,characteristic_uuid,success,failure)
- device_id: 設(shè)備藍牙的UUID或設(shè)備的mac地址
- service_uuid: 藍牙服務(wù)的UUID
- characteristic_uuid: 具有特征值的藍牙的UUID
- data: 利用數(shù)組存儲的二進制數(shù)
- success: 成功回調(diào)
- failure: 失敗回調(diào)
isConnected 是否連接:
ble.isConnected(device_id, success, failure);
參數(shù):
- device_id: 設(shè)備的uuid或者mac地址
- success: 成功回調(diào)
- failure: 失敗回調(diào)
example:
ble.isConnected(
'FFCA0B09-CB1D-4DC0-A1EF-31AFD3EDFB53',
function() {
console.log("Peripheral is connected");
},
function() {
console.log("Peripheral is *not* connected");
}
);
isEnabled 是否開啟藍牙:
ble.isEnabled(success, failure);
參數(shù):
- success: 成功回調(diào)
- failure: 失敗回調(diào)
example:
ble.isEnabled(
function() {
console.log("Bluetooth is enabled");
},
function() {
console.log("Bluetooth is *not* enabled");
}
);
startStateNotification 打開通知狀態(tài):
ble.startStateNotifications(success, failure);
狀態(tài):
- "on"
- "off"
- "turningOn" (Android Only)
- "turningOff" (Android Only)
- "unknown" (iOS Only)
- "resetting" (iOS Only)
- "unsupported" (iOS Only)
- "unauthorized" (iOS Only)
參數(shù):
- success: 成功回調(diào)
- failure: 失敗回調(diào)
example:
ble.startStateNotifications(
function(state) {
console.log("Bluetooth is " + state);
}
);
stopStateNotification 停止狀態(tài)通知:
ble.startStateNotifications(success, failure);
showBluetoothSettings 顯示藍牙設(shè)置:
ble.showBluetoothSettings(success, failure);
enable 打開藍牙:
enable只能在Android平臺使用庭猩,iOS無法使用窟她。如果藍牙已經(jīng)打開,成功回調(diào)函數(shù)無法調(diào)用
ble.enable(success, failure);
參數(shù):
- success: 成功回調(diào)
- failure: 失敗回調(diào)
example:
ble.enable(
function() {
console.log("Bluetooth is enabled");
},
function() {
console.log("The user did *not* enable Bluetooth");
}
);
(如需轉(zhuǎn)載請注明)