W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎勵
通過 BLE 掃描和廣播提供的開放能力,可以根據(jù)指定狀態(tài)獲取外圍設(shè)備、啟動或停止 BLE 掃描、廣播。
接口名 | 功能描述 |
---|---|
startScan(List<BleScanFilter> filters) | 進(jìn)行 BLE 藍(lán)牙掃描,并使用 filters 對結(jié)果進(jìn)行過濾。 |
stopScan() | 停止 BLE 藍(lán)牙掃描。 |
getDevicesByStates(int[] states) | 根據(jù)狀態(tài)獲取連接的外圍設(shè)備。 |
BleCentralManager(BleCentralManagerCallback callback) | 獲取中心設(shè)備管理對象。 |
接口名 | 功能描述 |
---|---|
onScanCallback(BleScanResult result) | 掃描到 BLE 設(shè)備的結(jié)果回調(diào)。 |
onStartScanFailed(int resultCode) | 啟動掃描失敗的回調(diào)。 |
接口名 | 功能描述 |
---|---|
BleAdvertiser(Context context, BleAdvertiseCallback callback) | 用于獲取廣播操作對象。 |
startAdvertising(BleAdvertiseSettings settings, BleAdvertiseData advData, BleAdvertiseData scanResponse) | 進(jìn)行 BLE 廣播,第一個參數(shù)為廣播參數(shù),第二個為廣播數(shù)據(jù),第三個參數(shù)是掃描和廣播數(shù)據(jù)參數(shù)的響應(yīng)。 |
stopAdvertising() | 停止 BLE 廣播。 |
startResultEvent(int result) | 廣播回調(diào)結(jié)果。 |
// 實(shí)現(xiàn)掃描回調(diào)
public class ScanCallback implements BleCentralManagerCallback{
List<BleScanResult>results = new ArrayList<BleScanResult>();
@Override
public void onScanCallback(BleScanResult var1) {
// 對掃描結(jié)果進(jìn)行處理
results.add(var1);
}
@Override
public void onStartScanFailed(int var1) {
HiLog.info(TAG,"Start Scan failed,Code:" + var1);
}
}
// 獲取中心設(shè)備管理對象
private ScanCallback centralManagerCallback = new ScanCallback();
private BleCentralManager centralManager = new BleCentralManager(centralManagerCallback);
// 創(chuàng)建掃描過濾器然后開始掃描
List<BleScanFilter> filters = new ArrayList<BleScanFilter>();
centralManager.startScan(filters);
// 實(shí)現(xiàn) BLE 廣播回調(diào)
private BleAdvertiseCallback advertiseCallback = new BleAdvertiseCallback() {
@Override
public void startResultEvent(int result) {
if(result == BleAdvertiseCallback.RESULT_SUCC){
// 開始 BLE 廣播成功
}
else {
// 開始 BLE 廣播失敗
}
}
};
// 獲取 BLE 廣播對象
private BleAdvertiser advertiser = new BleAdvertiser(this,advertiseCallback);
// 創(chuàng)建 BLE 廣播參數(shù)和數(shù)據(jù)
private BleAdvertiseData data = new BleAdvertiseData.Builder()
.addServiceUuid(SequenceUuid.uuidFromString(Server_UUID)) // 添加服務(wù)的 UUID
.addServiceData(SequenceUuid.uuidFromString(Server_UUID),new byte[]{0x11}) // 添加廣播數(shù)據(jù)內(nèi)容
.build();
private BleAdvertiseSettings advertiseSettings = new BleAdvertiseSettings.Builder()
.setConnectable(true) // 設(shè)置是否可連接廣播
.setInterval(BleAdvertiseSettings.INTERVAL_SLOT_DEFAULT) // 設(shè)置廣播間隔
.setTxPower(BleAdvertiseSettings.TX_POWER_DEFAULT) // 設(shè)置廣播功率
.build();
// 開始廣播
advertiser.startAdvertising(advertiseSettings,data,null);
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: