android中获取mac地址8.0,关于Android8.0以下手机获取蓝牙Mac地址的问题和扫描周围的手机蓝牙问题 下篇...
接上篇,接下來我們就來看一下如何掃描到周圍的手機藍牙,主要是經典藍牙
// 廣播接收發現藍牙設備
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
Log.i("Lyb", "開始掃描...");
}
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (device != null) {
if (device.getType() != BluetoothDevice.DEVICE_TYPE_LE)
Log.i("Lyb", "discovery==" + "設備名:" + device.getName() + "\n設備地址:" + device.getAddress());
}
}
if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
Log.i("Lyb", "掃描結束.");
//
}
}
};
然后
// 注冊廣播接收器。
// 接收藍牙發現
IntentFilter filterFound = new IntentFilter(BluetoothDevice.ACTION_FOUND);
getPageContext().registerReceiver(mReceiver, filterFound);
IntentFilter filterStart = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
getPageContext().registerReceiver(mReceiver, filterStart);
IntentFilter filterFinish = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
getPageContext().registerReceiver(mReceiver, filterFinish);
try {
Method setDiscoverableTimeout = BluetoothAdapter.class.getMethod("setDiscoverableTimeout", int.class);
setDiscoverableTimeout.setAccessible(true);
Method setScanMode = BluetoothAdapter.class.getMethod("setScanMode", int.class, int.class);
setScanMode.setAccessible(true);
setDiscoverableTimeout.invoke(BluetoothAdapter.getDefaultAdapter(), Integer.MAX_VALUE);
setScanMode.invoke(BluetoothAdapter.getDefaultAdapter(), BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, Integer.MAX_VALUE);
} catch (Exception e) {
e.printStackTrace();
}
最后
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBluetoothAdapter.startDiscovery();
好了這樣就能獲取到最近的藍牙設備了
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的android中获取mac地址8.0,关于Android8.0以下手机获取蓝牙Mac地址的问题和扫描周围的手机蓝牙问题 下篇...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android最佳活动启动方法,026-
- 下一篇: android关机菜单修改,Androi