Android操作蓝牙
1检测一个设备是否有蓝牙设备,开启蓝牙设备,获取配对的设备
这些操作需要权限
<uses-permission android:name="android.permission.BLUETOOTH"/>
下面是判断是否有蓝牙设备,是否开启蓝牙,若没有开启则请求开启蓝牙
- <span style="font-family: SimSun; font-size: 18px;">BluetoothAdapter ba=BluetoothAdapter.getDefaultAdapter();
- if(null!=ba)
- {
- System.out.println("本机有蓝牙设备");
- if(!ba.isEnabled())
- {
- </span><span style="font-family: SimSun; font-size: 18px;"> Intent intent=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
- startActivity(intent);//或者ba.enable(); //同样的关闭WIFi为ba.disable();
- }
- }</span>
BluetoothAdapter ba=BluetoothAdapter.getDefaultAdapter();
if(null!=ba)
{
System.out.println("本机有蓝牙设备");
if(!ba.isEnabled())
{
Intent intent=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(intent);//或者ba.enable(); //同样的关闭WIFi为ba.disable();
}
}
下面的代码是获取已经配对的蓝牙设备
- <span style="font-family: SimSun; font-size: 18px;">Set<BluetoothDevice> device=ba.getBondedDevices();
- if(device.size()>0)
- {
- for(BluetoothDevice bd:device)
- {
- System.out.println(bd.getAddress()+bd.getName());
- }
- }
- </span>
Set<BluetoothDevice> device=ba.getBondedDevices();
if(device.size()>0)
{
for(BluetoothDevice bd:device)
{
System.out.println(bd.getAddress()+bd.getName());
}
}
2.设置蓝牙的可见时间,以便被其他设备发现并连接
需要权限
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
设置蓝牙的可见时间
- Intent intentvisible=new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
- intentvisible.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 400);
- //这个值么默认120秒,超过300秒将会被设置为300.可是在我的设备上是2400也行。API出错?
- this.startActivity(intentvisible);
Intent intentvisible=new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
intentvisible.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 400);
//这个值么默认120秒,超过300秒将会被设置为300.可是在我的设备上是2400也行。API出错?
this.startActivity(intentvisible);
3.获取扫描到的设备
需要权限
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
开始扫描的代码
- BluetoothAdapter ba=BluetoothAdapter.getDefaultAdapter();
- ba.startDiscovery();
BluetoothAdapter ba=BluetoothAdapter.getDefaultAdapter();
ba.startDiscovery();
消耗12秒的时间,耗电量大
当扫描到广播后,会发送广播消息,注册一个广播来接收
- class MyReceiver extends BroadcastReceiver
- {
- @Override
- public void onReceive(Context context, Intent intent) {
- BluetoothDevice bd=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
- Toast.makeText(BluetoothActivity.this, bd.getName(), Toast.LENGTH_SHORT).show();
- }
- }
class MyReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
BluetoothDevice bd=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Toast.makeText(BluetoothActivity.this, bd.getName(), Toast.LENGTH_SHORT).show();
}
}
注册这个广播
- IntentFilter intentfilter=new IntentFilter(BluetoothDevice.ACTION_FOUND);
- MyReceiver mr=new MyReceiver();
- this.registerReceiver(mr, intentfilter);
IntentFilter intentfilter=new IntentFilter(BluetoothDevice.ACTION_FOUND);
MyReceiver mr=new MyReceiver();
this.registerReceiver(mr, intentfilter);
注册一个扫描完毕的广播:
- class FinishFound extends BroadcastReceiver
- {
- @Override
- public void onReceive(Context context, Intent intent) {
- BluetoothActivity.this.unregisterReceiver(this);
- BluetoothActivity.this.unregisterReceiver(mr);
- }
- }
class FinishFound extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
BluetoothActivity.this.unregisterReceiver(this);
BluetoothActivity.this.unregisterReceiver(mr);
}
}
在这个广播中关闭扫描到蓝牙通知的广播,并且关闭自己
注册这个广播我就不说了