android 判断是否正在扫描蓝牙_判断蓝牙是否连接
Android對于藍牙開發(fā)從2.0版本的sdk才開始支持,而且模擬器不支持,測試至少需要兩部手機,所以制約了很多技術(shù)人員的開發(fā)。
1. 首先,要操作藍牙,先要在AndroidManifest.xml里加入權(quán)限
// 管理藍牙設(shè)備的權(quán)限
// 使用藍牙設(shè)備的權(quán)限
2.打開藍牙
獲得藍牙適配器(android.bluetooth.BluetoothAdapter),檢查該設(shè)備是否支持藍牙,如果支持,就打開藍牙。
[java]
// 檢查設(shè)備是否支持藍牙
adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter == null)
{
// 設(shè)備不支持藍牙
}
// 打開藍牙
if (!adapter.isEnabled())
{
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
// 設(shè)置藍牙可見性,最多300秒
intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
context.startActivity(intent);
}
// 檢查設(shè)備是否支持藍牙
adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter == null)
{
// 設(shè)備不支持藍牙
}
// 打開藍牙
if (!adapter.isEnabled())
{
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
// 設(shè)置藍牙可見性,最多300秒
intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
context.startActivity(intent);
}
3.獲取已配對的藍牙設(shè)備(android.bluetooth.BluetoothDevice)
首次連接某藍牙設(shè)備需要先配對,一旦配對成功,該設(shè)備的信息會被保存,以后連接時無需再配對,所以已配對的設(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.搜索周圍的藍牙設(shè)備
適配器搜索藍牙設(shè)備后將結(jié)果以廣播形式傳出去,所以需要自定義一個繼承廣播的類,在onReceive方法中獲得并處理藍牙設(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);
// 注冊廣播接收器,接收并處理搜索結(jié)果
context.registerReceiver(receiver, intentFilter);
// 尋找藍牙設(shè)備,android會將查找到的設(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);
// 注冊廣播接收器,接收并處理搜索結(jié)果
context.registerReceiver(receiver, intentFilter);
// 尋找藍牙設(shè)備,android會將查找到的設(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.藍牙設(shè)備的配對和狀態(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)) {
// 獲取查找到的藍牙設(shè)備
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
System.out.println(device.getName());
// 如果查找到的設(shè)備符合要連接的設(shè)備,處理
if (device.getName().equalsIgnoreCase(name)) {
// 搜索藍牙設(shè)備的過程占用資源比較多,一旦找到需要連接的設(shè)備后需要及時關(guān)閉搜索
adapter.cancelDiscovery();
// 獲取藍牙設(shè)備的連接狀態(tài)
connectState = device.getBondState();
switch (connectState) {
// 未配對
case BluetoothDevice.BOND_NONE:
// 配對
try {
Method createBondMethod = BluetoothDevice.class.getMethod("createBond");
createBondMethod.invoke(device);
} catch (Exception e) {
e.printStackTrace();
}
break;
// 已配對
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)) {
// 獲取查找到的藍牙設(shè)備
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
System.out.println(device.getName());
// 如果查找到的設(shè)備符合要連接的設(shè)備,處理
if (device.getName().equalsIgnoreCase(name)) {
// 搜索藍牙設(shè)備的過程占用資源比較多,一旦找到需要連接的設(shè)備后需要及時關(guān)閉搜索
adapter.cancelDiscovery();
// 獲取藍牙設(shè)備的連接狀態(tài)
connectState = device.getBondState();
switch (connectState) {
// 未配對
case BluetoothDevice.BOND_NONE:
// 配對
try {
Method createBondMethod = BluetoothDevice.class.getMethod("createBond");
createBondMethod.invoke(device);
} catch (Exception e) {
e.printStackTrace();
}
break;
// 已配對
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.藍牙設(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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vscode 新建cpp文件_Visua
- 下一篇: python map reduce fi