这两个 API 和Socket 和ServerSocket 差不多 这里不予介绍
当然了,Android 中蓝牙相关的API还很多!这里我只说了几个常用的!!!!
Android 中的实战** 现在我们就按照正常的逻辑来走! **
第一步我们要先得到 蓝牙本地适配器,来判断设备是否支持蓝牙!
mAdapter=BluetoothAdapter.getDefaultAdapter();//判断设备是否支持蓝牙if (mAdapter==null){Toast.makeText(this, "不支持蓝牙", Toast.LENGTH_SHORT).show();}
第二步好了!现在下面要做的就是判断蓝牙是否打开了!没有打开则打开!
if (!mAdapter.isEnabled()){//蓝牙没有打开// mAdapter.enable();//不支持这样写,影响用户体验//建议使用这种方式Intent intent=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);startActivityForResult(intent, 200);}
代码写得很清楚!我就不解释啦!!!
第三步成功打开蓝牙之后,要想别人的适配上能够发现,我们得设置蓝牙的可见性,现在我就来设置蓝牙的可见性!看代码
@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);if (requestCode==200){Intent dis=new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);dis.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);startActivity(dis);}}
不知大家有没有注意到,我是在 onActivityResult(int requestCode, int resultCode, Intent data) 这个回调函数中设置蓝牙可见性的!因为我们在打开蓝牙的时候用了 startActivityForResult(Intent intent,int requestCode) 这个方法!还有一点大家也要注意 就是蓝牙在设置可见性的时候,有个时常。就是这个字段 BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION 对应的值!这个最大值是300秒!
第四步至此,我们就可以搜索附近的蓝牙啦!那么有同学会问了,我们怎么知道有设备被搜索到呢?这个很简单哦!但我们搜索到设备的时候,系统就会给我们发送广播啦!我们只有注册个广播接收器就行啦!
mAdapter.startDiscovery();//开始搜索//注册蓝牙广播接收器。IntentFilter filter = new IntentFilter();filter.addAction(BluetoothDevice.ACTION_FOUND);//发现蓝牙动作filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);//搜索结束动作filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);//搜素开始动作registerReceiver(blueReceiver, filter);/*** 蓝牙接收器**/public class BlueReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {if (intent.getAction().equals(BluetoothDevice.ACTION_FOUND)) {BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);deviceList.add(device);adapter.notifyDataSetChanged();}if (intent.getAction().equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) {}if (intent.getAction().equals(BluetoothAdapter.ACTION_DISCOVERY_STARTED)) {}}}
在 BlueReceiver 中我只是简单的把搜索到得设备放到集合中。被没有做过多的操作!
第五步现在到了比较重要的步骤了!当我们搜索到了蓝牙的之后,我要配对,因为只有在配对之后才能连接哦!
BluetoothDevice device = (BluetoothDevice) adapter.getItem(i);if (device.getBondState() == BluetoothDevice.BOND_BONDED) {//是否已配对connect(device);} else {try {Method boned=device.getClass().getMethod("createBond");boolean isok= (boolean) boned.invoke(device);if(isok){connect(device);}} catch (Exception e) {e.printStackTrace();}}
这里,我想说明的是就是这个配对,在 API19 之后 createBond() Android对外提供了这个方法!但是在 API19 以前并没有提供这个方法,所以呢我只能用反射了!这个兼容性比较好奥!!
第六步现在,就很简答啦!只要连接就行啦!在这里我放在了一个线程中了!
private class ConnectThread extends Thread {private final BluetoothSocket mmSocket;private final BluetoothDevice mmDevice;public ConnectThread(BluetoothDevice device) {mmDevice = device;BluetoothSocket tmp = null;try {tmp = device.createRfcommSocketToServiceRecord(MY_UUID);} catch (IOException e) {Log.e(TAG, "create() failed", e);}mmSocket = tmp;}@Overridepublic void run() {Log.i(TAG, "BEGIN mConnectThread");setName("ConnectThread");mAdapter.cancelDiscovery();try {mmSocket.connect();} catch (IOException e) {connectionFailed();try {mmSocket.close();} catch (IOException e2) {Log.e(TAG, "unable to close() socket during connection failure", e2);}BlueToothService.this.start();return;}synchronized (BlueToothService.this) {mConnectThread = null;}connected(mmSocket, mmDevice);//这里是另外一个读取数据的线程,这里就和socket操作一样了write("start".getBytes());}public void cancel() {try {mmSocket.close();} catch (IOException e) {Log.e(TAG, "close() of connect socket failed", e);}}}
好啦!如果你读到这里 就也就学会了蓝牙的简单操作了!