Android开发学习之蓝牙API

        1、显式开启蓝牙设备

Intent intent=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent,1);
        2、隐式开启/关闭蓝牙设备

BluetoothAdapter mAdapter=BluetoothAdapter.getDefaultAdapter();
//开启蓝牙设备
mAdapter.enable();
//关闭蓝牙设备
mAdapter.disable();
       3、获取已配对的蓝牙设备

Set<BluetoothDevice> mDevices=mAdapter.getBondedDevices();
//获取已配对的设备
if(mDevices.size()>0)
{
   for(BluetoothDevice mDevice : mDevices)
   {
	mView.append(mDevice.getName()+":"+mDevice.getAddress()+"\n");
   }
}
      4、搜索蓝牙设备

private final BroadcastReceiver receiver=new BroadcastReceiver()
{
    @Override
    public void onReceive(Context mContext, Intent intent) 
    {
	if(BluetoothDevice.ACTION_FOUND.equals(intent.getAction()))
	{
	   BluetoothDevice mDevice=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
	   if(mDevice.getBondState()!=BluetoothDevice.BOND_BONDED)
	   {
		mView.append(mDevice.getName()+":"+mDevice.getAddress()+"\n");
	   }
	}else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(intent.getAction()))
	{
		MainActivity.this.setProgressBarIndeterminateVisibility(false);
	}
     }
};


//蓝牙动作监听广播
IntentFilter mFilter=new IntentFilter(BluetoothDevice.ACTION_FOUND);
this.registerReceiver(receiver, mFilter);
//蓝牙动作监听结束广播
mFilter=new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
this.registerReceiver(receiver, mFilter);

        


Android开发学习之蓝牙API,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。