- 藍(lán)牙開發(fā)「防丟器」的相關(guān)知識(shí)點(diǎn)(一):掃描并識(shí)別設(shè)備
- 藍(lán)牙開發(fā)「防丟器」的相關(guān)知識(shí)點(diǎn)(二):連接設(shè)備并檢測連接狀態(tài)
- 藍(lán)牙開發(fā)「防丟器」的相關(guān)知識(shí)點(diǎn)(三):手機(jī)與設(shè)備之間指令傳輸
1.手機(jī)發(fā)送指令到BLE設(shè)備
手機(jī)向藍(lán)牙設(shè)備發(fā)送指令鳄炉,調(diào)用的是BluetoothGatt.writeCharacteristic(BluetoothGattCharacteristic characteristic)方法宦搬,本項(xiàng)目中由于涉及多設(shè)備管理,故由一個(gè)統(tǒng)一的工具類來發(fā)送指令。
在Activity中發(fā)送指令:
public static final UUID UUID_S_EXTRA = UUID.fromString("0000ff00-0000-1000-8000-00805f9b34fb");//擴(kuò)展服務(wù)
public static final UUID UUID_S_EXTRA_C = UUID.fromString("0000fff0-0000-1000-8000-00805f9b34fb");//設(shè)備設(shè)置
public static final byte[] VALUE_FIND_LIGHT_ON = {(byte) 0x03, (byte) 0x01, (byte) 0x30}; // 指示燈打開
public static final byte[] VALUE_FIND_LIGHT_OFF = {(byte) 0x03, (byte) 0x00, (byte) 0x30}; // 指示燈關(guān)閉
byte[] value = BluetoothUtils.VALUE_FIND_LIGHT_ON;
buttonView.setChecked(!isChecked);
BluetoothUtils.sendValueToBle(mMac, BluetoothUtils.UUID_S_EXTRA, BluetoothUtils.UUID_S_EXTRA_C, value);
** 發(fā)送指令時(shí)站蝠,首先根據(jù)mac找出該設(shè)備對(duì)應(yīng)的BluetoothGatt揽祥,然后發(fā)送對(duì)應(yīng)的參數(shù)。*
2.判斷指令發(fā)送是否成功
對(duì)于手機(jī)已經(jīng)發(fā)送出去的指令桨醋,如何判斷設(shè)備是否接收到并生效比較關(guān)鍵棚瘟,這與很多業(yè)務(wù)相關(guān)聯(lián)。這一點(diǎn)喜最,在上篇文章中也提到偎蘸,我們使用BluetoothGattCallback onCharacteristicWrite來解決,設(shè)備收到指令后返顺,會(huì)進(jìn)入該回調(diào)參數(shù)禀苦,我們可以獲取被寫入指令設(shè)備的MAC蔓肯,寫入的值遂鹊,寫入的狀態(tài),寫入的通道等信息蔗包,據(jù)此來判斷是否成功秉扑。
3.手機(jī)獲取BLE設(shè)備的指令
手機(jī)與設(shè)備建立連接后,可訂閱設(shè)備相應(yīng)的通道(如電量调限、按鍵指令等)舟陆,訂閱后,方可接收到設(shè)備發(fā)送的指令信息耻矮,該信息從BluetoothGattCallback onCharacteristicRead獲取秦躯,獲取數(shù)據(jù)后的處理,可參與上篇文章裆装。
4附錄:指令發(fā)送相關(guān)的工具類BluetoothUtils.java
package com.powerstick.beaglepro.util;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import com.afap.utils.ByteUtils;
import com.powerstick.beaglepro.MyApplication;
import com.powerstick.beaglepro.services.BluetoothLeService;
import com.tencent.bugly.crashreport.BuglyLog;
import java.util.UUID;
public final class BluetoothUtils {
public static final UUID UUID_S_IMMEDIATE = UUID.fromString("00001802-0000-1000-8000-00805f9b34fb");//立即報(bào)警
public static final UUID UUID_S_IMMEDIATE_C_ALERT = UUID.fromString("00002a06-0000-1000-8000-00805f9b34fb");//報(bào)警級(jí)別
public static final UUID UUID_S_BATTERY = UUID.fromString("0000180f-0000-1000-8000-00805f9b34fb");//電池
public static final UUID UUID_S_BATTERY_C_LEVEL = UUID.fromString("00002a19-0000-1000-8000-00805f9b34fb");//電池level
public static final UUID UUID_S_DEVICEINFO = UUID.fromString("0000180a-0000-1000-8000-00805f9b34fb");//設(shè)備信息
public static final UUID UUID_S_DEVICEINFO_C_FIRMWARE = UUID.fromString("00002a28-0000-1000-8000-00805f9b34fb");//固件
public static final UUID UUID_S_KEY = UUID.fromString("0000ffe0-0000-1000-8000-00805f9b34fb");//按鍵信息
public static final UUID UUID_S_KEY_C_PRESS = UUID.fromString("0000ffe1-0000-1000-8000-00805f9b34fb");//按鍵
public static final UUID UUID_S_EXTRA = UUID.fromString("0000ff00-0000-1000-8000-00805f9b34fb");//擴(kuò)展服務(wù)
public static final UUID UUID_S_EXTRA_C = UUID.fromString("0000fff0-0000-1000-8000-00805f9b34fb");//設(shè)備設(shè)置
public static final UUID UUID_S_EXTRA_C_LOGIN = UUID.fromString("0000ffc0-0000-1000-8000-00805f9b34fb");//認(rèn)證
public static final UUID UUID_S_EXTRA_C_UNBIND = UUID.fromString("0000ffc1-0000-1000-8000-00805f9b34fb");//解綁
public static final UUID UUID_S_EXTRA_C_RENAME = UUID.fromString("0000fff3-0000-1000-8000-00805f9b34fb");//重命名
public static final byte[] VALUE_MODE_THETHER = {(byte) 0x01, (byte) 0x00, (byte) 0x10};
public static final byte[] VALUE_MODE_FIND = {(byte) 0x01, (byte) 0x01, (byte) 0x10};
public static final byte[] VALUE_IMMEDIATE_ON = {(byte) 2}; // 立即報(bào)警
public static final byte[] VALUE_IMMEDIATE_OFF = {(byte) 0}; // 取消立即報(bào)警
public static final byte[] VALUE_TETHER_BEEP_ON = {(byte) 0x02, (byte) 0x01, (byte) 0x20}; //警報(bào)打開
public static final byte[] VALUE_TETHER_BEEP_OFF = {(byte) 0x02, (byte) 0x00, (byte) 0x20}; //警報(bào)關(guān)閉
public static final byte[] VALUE_FIND_LIGHT_ON = {(byte) 0x03, (byte) 0x01, (byte) 0x30}; //指示燈打開
public static final byte[] VALUE_FIND_LIGHT_OFF = {(byte) 0x03, (byte) 0x00, (byte) 0x30}; //指示燈關(guān)閉
public static final byte[] VALUE_UNBIND = {(byte) 0x09, (byte) 0x01, (byte) 0x90};// 解綁指令
public final static int REQUEST_ENABLE_BT = 2001;
private final Context mActivity;
private final BluetoothAdapter mBluetoothAdapter;
public BluetoothUtils(final Context activity) {
mActivity = activity;
final BluetoothManager btManager = (BluetoothManager) mActivity.getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = btManager.getAdapter();
}
public void askUserToEnableBluetoothIfNeeded() {
if (isBluetoothLeSupported() && (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled())) {
final Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
((Activity) mActivity).startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
public BluetoothAdapter getBluetoothAdapter() {
return mBluetoothAdapter;
}
public boolean isBluetoothLeSupported() {
return mActivity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE);
}
public boolean isBluetoothOn() {
if (mBluetoothAdapter == null) {
return false;
} else {
return mBluetoothAdapter.isEnabled();
}
}
public static BluetoothLeService getBleService() {
return MyApplication.getInstance().mBluetoothLeService;
}
public static boolean sendValueToBle(String mac, UUID serviceId, UUID characteristicId, byte[] value) {
if (getBleService() == null) {
BuglyLog.e("BluetoothUtils", "藍(lán)牙服務(wù)為null");
return false;
}
boolean result = false;
BluetoothGattService mService = getBleService().getService(mac, serviceId);
if (mService != null) {
BluetoothGattCharacteristic mCharacteristic = mService.getCharacteristic(characteristicId);
if (mCharacteristic != null) {
mCharacteristic.setValue(value);
BuglyLog.w("BluetoothUtils", ByteUtils.byteArrayToHexString(value));
result = getBleService().writeCharacteristic(mac, mCharacteristic);
} else {
BuglyLog.w("BluetoothUtils", "目標(biāo)通道為NULL");
}
}
return result;
}
public static void readCharacteristic(String mac, UUID serviceId, UUID characteristicId) {
if (getBleService() == null) {
return;
}
BluetoothGattService mService = getBleService().getService(mac, serviceId);
if (mService != null) {
BluetoothGattCharacteristic mCharacteristic = mService.getCharacteristic(characteristicId);
if (mCharacteristic != null) {
BuglyLog.w("BluetoothUtils", "讀弱獬小:" + characteristicId.toString());
getBleService().readCharacteristic(mac, mCharacteristic);
} else {
BuglyLog.w("BluetoothUtils", "目標(biāo)通道為NULL");
}
}
}
public static void readRemoteRssi(String mac) {
if (getBleService() == null) {
return;
}
BluetoothGatt gatt = getBleService().getGatt(mac);
if (gatt != null) {
gatt.readRemoteRssi();
}
}
}