日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android 判断是否正在扫描蓝牙_判断蓝牙是否连接

發(fā)布時(shí)間:2025/3/19 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 判断是否正在扫描蓝牙_判断蓝牙是否连接 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Android對(duì)于藍(lán)牙開發(fā)從2.0版本的sdk才開始支持,而且模擬器不支持,測(cè)試至少需要兩部手機(jī),所以制約了很多技術(shù)人員的開發(fā)。

1. 首先,要操作藍(lán)牙,先要在AndroidManifest.xml里加入權(quán)限

// 管理藍(lán)牙設(shè)備的權(quán)限

// 使用藍(lán)牙設(shè)備的權(quán)限

2.打開藍(lán)牙

獲得藍(lán)牙適配器(android.bluetooth.BluetoothAdapter),檢查該設(shè)備是否支持藍(lán)牙,如果支持,就打開藍(lán)牙。

[java]

// 檢查設(shè)備是否支持藍(lán)牙

adapter = BluetoothAdapter.getDefaultAdapter();

if (adapter == null)

{

// 設(shè)備不支持藍(lán)牙

}

// 打開藍(lán)牙

if (!adapter.isEnabled())

{

Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

// 設(shè)置藍(lán)牙可見性,最多300秒

intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);

context.startActivity(intent);

}

// 檢查設(shè)備是否支持藍(lán)牙

adapter = BluetoothAdapter.getDefaultAdapter();

if (adapter == null)

{

// 設(shè)備不支持藍(lán)牙

}

// 打開藍(lán)牙

if (!adapter.isEnabled())

{

Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

// 設(shè)置藍(lán)牙可見性,最多300秒

intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);

context.startActivity(intent);

}

3.獲取已配對(duì)的藍(lán)牙設(shè)備(android.bluetooth.BluetoothDevice)

首次連接某藍(lán)牙設(shè)備需要先配對(duì),一旦配對(duì)成功,該設(shè)備的信息會(huì)被保存,以后連接時(shí)無需再配對(duì),所以已配對(duì)的設(shè)備不一定是能連接的。

[java]

BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();

Set devices = adapter.getBondedDevices();

for(int i=0; i

{

BluetoothDevice device = (BluetoothDevice) devices.iterator().next();

System.out.println(device.getName());

}

BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();

Set devices = adapter.getBondedDevices();

for(int i=0; i

{

BluetoothDevice device = (BluetoothDevice) devices.iterator().next();

System.out.println(device.getName());

}

4.搜索周圍的藍(lán)牙設(shè)備

適配器搜索藍(lán)牙設(shè)備后將結(jié)果以廣播形式傳出去,所以需要自定義一個(gè)繼承廣播的類,在onReceive方法中獲得并處理藍(lán)牙設(shè)備的搜索結(jié)果。

[java]

// 設(shè)置廣播信息過濾

IntentFilter intentFilter = new IntentFilter();

intentFilter.addAction(BluetoothDevice.ACTION_FOUND);

intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);

intentFilter.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);

intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);

// 注冊(cè)廣播接收器,接收并處理搜索結(jié)果

context.registerReceiver(receiver, intentFilter);

// 尋找藍(lán)牙設(shè)備,android會(huì)將查找到的設(shè)備以廣播形式發(fā)出去

adapter.startDiscovery();

// 設(shè)置廣播信息過濾

IntentFilter intentFilter = new IntentFilter();

intentFilter.addAction(BluetoothDevice.ACTION_FOUND);

intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);

intentFilter.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);

intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);

// 注冊(cè)廣播接收器,接收并處理搜索結(jié)果

context.registerReceiver(receiver, intentFilter);

// 尋找藍(lán)牙設(shè)備,android會(huì)將查找到的設(shè)備以廣播形式發(fā)出去

adapter.startDiscovery(); 自定義廣播類

[java]

private BroadcastReceiver receiver = new BroadcastReceiver() {

@Override

public void onReceive(Context context, Intent intent) {

String action = intent.getAction();

if (BluetoothDevice.ACTION_FOUND.equals(action)) {

BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

System.out.println(device.getName());

}

}

}

private BroadcastReceiver receiver = new BroadcastReceiver() {

@Override

public void onReceive(Context context, Intent intent) {

String action = intent.getAction();

if (BluetoothDevice.ACTION_FOUND.equals(action)) {

BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

System.out.println(device.getName());

}

}

}

5.藍(lán)牙設(shè)備的配對(duì)和狀態(tài)監(jiān)視

[java]

private BroadcastReceiver receiver = new BroadcastReceiver() {

@Override

public void onReceive(Context context, Intent intent) {

String action = intent.getAction();

if (BluetoothDevice.ACTION_FOUND.equals(action)) {

// 獲取查找到的藍(lán)牙設(shè)備

BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

System.out.println(device.getName());

// 如果查找到的設(shè)備符合要連接的設(shè)備,處理

if (device.getName().equalsIgnoreCase(name)) {

// 搜索藍(lán)牙設(shè)備的過程占用資源比較多,一旦找到需要連接的設(shè)備后需要及時(shí)關(guān)閉搜索

adapter.cancelDiscovery();

// 獲取藍(lán)牙設(shè)備的連接狀態(tài)

connectState = device.getBondState();

switch (connectState) {

// 未配對(duì)

case BluetoothDevice.BOND_NONE:

// 配對(duì)

try {

Method createBondMethod = BluetoothDevice.class.getMethod("createBond");

createBondMethod.invoke(device);

} catch (Exception e) {

e.printStackTrace();

}

break;

// 已配對(duì)

case BluetoothDevice.BOND_BONDED:

try {

// 連接

connect(device);

} catch (IOException e) {

e.printStackTrace();

}

break;

}

}

} else if(BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {

// 狀態(tài)改變的廣播

BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

if (device.getName().equalsIgnoreCase(name)) {

connectState = device.getBondState();

switch (connectState) {

case BluetoothDevice.BOND_NONE:

break;

case BluetoothDevice.BOND_BONDING:

break;

case BluetoothDevice.BOND_BONDED:

try {

// 連接

connect(device);

} catch (IOException e) {

e.printStackTrace();

}

break;

}

}

}

}

}

private BroadcastReceiver receiver = new BroadcastReceiver() {

@Override

public void onReceive(Context context, Intent intent) {

String action = intent.getAction();

if (BluetoothDevice.ACTION_FOUND.equals(action)) {

// 獲取查找到的藍(lán)牙設(shè)備

BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

System.out.println(device.getName());

// 如果查找到的設(shè)備符合要連接的設(shè)備,處理

if (device.getName().equalsIgnoreCase(name)) {

// 搜索藍(lán)牙設(shè)備的過程占用資源比較多,一旦找到需要連接的設(shè)備后需要及時(shí)關(guān)閉搜索

adapter.cancelDiscovery();

// 獲取藍(lán)牙設(shè)備的連接狀態(tài)

connectState = device.getBondState();

switch (connectState) {

// 未配對(duì)

case BluetoothDevice.BOND_NONE:

// 配對(duì)

try {

Method createBondMethod = BluetoothDevice.class.getMethod("createBond");

createBondMethod.invoke(device);

} catch (Exception e) {

e.printStackTrace();

}

break;

// 已配對(duì)

case BluetoothDevice.BOND_BONDED:

try {

// 連接

connect(device);

} catch (IOException e) {

e.printStackTrace();

}

break;

}

}

} else if(BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {

// 狀態(tài)改變的廣播

BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

if (device.getName().equalsIgnoreCase(name)) {

connectState = device.getBondState();

switch (connectState) {

case BluetoothDevice.BOND_NONE:

break;

case BluetoothDevice.BOND_BONDING:

break;

case BluetoothDevice.BOND_BONDED:

try {

// 連接

connect(device);

} catch (IOException e) {

e.printStackTrace();

}

break;

}

}

}

}

} 6.藍(lán)牙設(shè)備的連接

[java]

private void connect(BluetoothDevice device) throws IOException {

// 固定的UUID

final String SPP_UUID = "00001101-0000-1000-8000-00805F9B34FB";

UUID uuid = UUID.fromString(SPP_UUID);

BluetoothSocket socket = device.createRfcommSocketToServiceRecord(uuid);

socket.connect();

}

private void connect(BluetoothDevice device) throws IOException {

// 固定的UUID

final String SPP_UUID = "00001101-0000-1000-8000-00805F9B34FB";

UUID uuid = UUID.fromString(SPP_UUID);

BluetoothSocket socket = device.createRfcommSocketToServiceRecord(uuid);

socket.connect();

}

總結(jié)

以上是生活随笔為你收集整理的android 判断是否正在扫描蓝牙_判断蓝牙是否连接的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。