android獲取附近藍(lán)牙設(shè)備并計(jì)算距離的實(shí)例代碼

需要用到本地藍(lán)牙適配器

為望都等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及望都網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都做網(wǎng)站、成都網(wǎng)站制作、望都網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!

// 獲取本地藍(lán)牙適配器
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

判斷是否支持藍(lán)牙,并確認(rèn)打開該功能。

// 判斷手機(jī)是否支持藍(lán)牙
 if (mBluetoothAdapter == null) {
  Toast.makeText(this, "設(shè)備不支持藍(lán)牙", Toast.LENGTH_SHORT).show();
  finish();
 }
 // 判斷是否打開藍(lán)牙
 if (!mBluetoothAdapter.isEnabled()) {
  // 彈出對(duì)話框提示用戶是后打開
  Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
  startActivityForResult(intent, 1);
  // 不做提示,強(qiáng)行打開
  // mBluetoothAdapter.enable();
 }else {
  // 不做提示,強(qiáng)行打開
   mBluetoothAdapter.enable();
 }

獲取手機(jī)已經(jīng)配對(duì)的藍(lán)牙設(shè)備

// 獲取已經(jīng)配對(duì)的設(shè)備
 Set<BluetoothDevice> pairedDevices = mBluetoothAdapter
   .getBondedDevices();
 // 判斷是否有配對(duì)過的設(shè)備
 if (pairedDevices.size() > 0) {
  for (BluetoothDevice device : pairedDevices) {
   // 遍歷
   mDevicesList.add(device.getAddress());
   tvDevices.append(device.getName() + ":" + device.getAddress() + "\n");
  }
 }

注冊(cè)異步搜索藍(lán)牙設(shè)備的廣播

// 找到設(shè)備的廣播
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
// 注冊(cè)廣播
registerReceiver(receiver, filter);
// 搜索完成的廣播
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
// 注冊(cè)廣播
registerReceiver(receiver, filter);

搜索藍(lán)牙的方法

 private void scanBluth() {
// 設(shè)置進(jìn)度條
setProgressBarIndeterminateVisibility(true);
setTitle("正在搜索...");
// 判斷是否在搜索,如果在搜索,就取消搜索
if (mBluetoothAdapter.isDiscovering()) {
mBluetoothAdapter.cancelDiscovery();
}
// 開始搜索
mBluetoothAdapter.startDiscovery();
}

廣播接收器

 private final BroadcastReceiver receiver = new BroadcastReceiver() {
 @Override
 public void onReceive(Context context, Intent intent) {
  // 收到的廣播類型
  String action = intent.getAction();
  // 發(fā)現(xiàn)設(shè)備的廣播
  if (BluetoothDevice.ACTION_FOUND.equals(action)) {
   // 從intent中獲取設(shè)備
   BluetoothDevice device = intent
     .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
   String aa = tvDevices.getText().toString() + "";
   if (aa.contains(device.getAddress())) {
    return;
   } else {
    // 判斷是否配對(duì)過
    if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
     // 添加到列表
     short rssi = intent.getExtras().getShort(
       BluetoothDevice.EXTRA_RSSI);
     int iRssi = abs(rssi);
 // 將藍(lán)牙信號(hào)強(qiáng)度換算為距離
     double power = (iRssi - 59) / 25.0;
     String mm = new Formatter().format("%.2f", pow(10, power)).toString();
     tvDevices.append(device.getName() + ":"
       + device.getAddress() + " :" + mm + "m" + "\n");
    }else {
    }
   }
   // 搜索完成
  } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED
    .equals(action)) {
   // 關(guān)閉進(jìn)度條
   setProgressBarIndeterminateVisibility(true);
   setTitle("搜索完成!");
   mBLHandler.sendEmptyMessageDelayed(1, 1000);
  }
 }
};

代碼里我添加了循環(huán)掃描的Handler

// 用于循環(huán)掃描藍(lán)牙的hangdler
Handler mBLHandler = new Handler() {
 @Override
 public void handleMessage(Message msg) {
  super.handleMessage(msg);
  switch (msg.what) {
   case 1:
    scanBluth();
    break;
   default:
    break;
  }
 }
};

項(xiàng)目里用到前期寫的一個(gè)權(quán)限管理器,具體去前邊看,地址:

https://www.jb51.net/article/133350.htm

用到的權(quán)限為

//所有手機(jī)需要的權(quán)限,藍(lán)牙功能才能正常使用
 <uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
//部分手機(jī)(如小米等)需要將下面兩個(gè)權(quán)限添加進(jìn)去,藍(lán)牙功能才能正常使用
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

以上這篇android獲取附近藍(lán)牙設(shè)備并計(jì)算距離的實(shí)例代碼就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持創(chuàng)新互聯(lián)。

當(dāng)前名稱:android獲取附近藍(lán)牙設(shè)備并計(jì)算距離的實(shí)例代碼
鏈接地址:http://muchs.cn/article46/iidseg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、搜索引擎優(yōu)化定制開發(fā)、網(wǎng)站導(dǎo)航、自適應(yīng)網(wǎng)站、外貿(mào)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計(jì)公司