您的位置:首页 > 移动开发 > Android开发

经典蓝牙总结(之前所有的笔记都放在未知笔记里面,但是看很多招聘上都要求博客,所以就算是从今天开始吧)

2015-04-22 12:01 483 查看

1.蓝牙的工作原理 

1.1 蓝牙通信的主从关系 

蓝牙技术规定每一对设备之间进行蓝牙通讯时,必须一个为主角色,另一为从角色,才能进行通信,通信时,必须由主端进行查找,发起配对,建链成功后,双方即可收发数据。理论上,一个蓝牙主端设备,可同时与7个蓝牙从端设备进行通讯。一个具备蓝牙通讯功能的设备, 可以在两个角色间切换,平时工作在从模式,等待其它主设备来连接,需要时,转换为主模式,向其它设备发起呼叫。一个蓝牙设备以主模式发起呼叫时,需要知道对方的蓝牙地址,配对密码等信息,配对完成后,可直接发起呼叫。  

1.2 蓝牙的呼叫过程 

蓝牙主端设备发起呼叫,首先是查找,找出周围处于可被查找的蓝牙设备。主端设备找到从端蓝牙设备后,与从端蓝牙设备进行配对,此时需要输入从端设备的PIN码,也有设备不需要输入PIN码。配对完成后,从端蓝牙设备会记录主端设备的信任信息,此时主端即可向从端设备发起呼叫,已配对的设备在下次呼叫时,不再需要重新配对。已配对的设备,做为从端的蓝牙耳机也可以发起建链请求,但做数据通讯的蓝牙模块一般不发起呼叫。链路建立成功后,主从两端之间即可进行双向的数据或语音通讯。在通信状态下,主端和从端设备都可以发起断链,断开蓝牙链路。  

1.3 蓝牙一对一的串口数据传输应用 

蓝牙数据传输应用中,一对一串口数据通讯是最常见的应用之一,蓝牙设备在出厂前即提前设好两个蓝牙设备之间的配对信息,主端预存有从端设备的PIN码、地址等,两端设备加电即自动建链,透明串口传输,无需外围电路干预。一对一应用中从端设备可以设为两种类型,一是静默状态,即只能与指定的主端通信,不被别的蓝牙设备查找;二是开发状态,既可被指定主端查找,也可以被别的蓝牙设备查找建链。 

2.蓝牙Android编程应用

2.1 蓝牙3.0及以下版本编程  

2.1.1认识一下UUID 

UUID含义是通用唯一识别码 (Universally Unique Identifier),这 是一个软件建构的标准,也是被开源软件基金会 (Open Software Foundation, OSF) 的组织应用在分布式计算环境 (Distributed Computing Environment, DCE) 领域的一部分。 在蓝牙3.0及一下版本中,UUID被用于唯一标识一个服务,比如文件传输服务,串口服务、打印机服务等,如下:#蓝牙串口服务 SerialPortServiceClass_UUID = '{00001101-0000-1000-8000-00805F9B34FB}' LANAccessUsingPPPServiceClass_UUID = '{00001102-0000-1000-8000-00805F9B34FB}' #拨号网络服务 DialupNetworkingServiceClass_UUID = '{00001103-0000-1000-8000-00805F9B34FB}' #信息同步服务 IrMCSyncServiceClass_UUID = '{00001104-0000-1000-8000-00805F9B34FB}' SDP_OBEXObjectPushServiceClass_UUID = '{00001105-0000-1000-8000-00805F9B34FB}' #文件传输服务 OBEXFileTransferServiceClass_UUID = '{00001106-0000-1000-8000-00805F9B34FB}'                                IrMCSyncCommandServiceClass_UUID = '{00001107-0000-1000-8000-00805F9B34FB}' 蓝牙的连接有主从设备,提供服务的可以认为是从设备。主设备通过UUID访问从设备提供具有相同UUID的服务,从而建立客服端—服务器(C/S)模式。

3.1.2 版本3.0蓝牙Android编程原理 

 使用蓝牙功能 ,首先要获得权限,蓝牙权限设置:
<uses-permission android:name="android.permission.BLUETOOTH" /><uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> 
A 建立并获得本地蓝牙适配器:
//2.判断获取到的adapter对象是否为空,如果空的话说明该设备没有蓝牙设备if (adapter!=null) {}else{Toast.makeText(this, "该设备没有蓝牙", 0).show();}
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
B 如果本地有蓝牙设备则开启。
if(mBluetoothAdapter !=null){//判断是否有蓝牙
if(!mBluetoothAdapter.isEnabled()){//判断蓝牙 是否开启,未开启则请用户开启
Intent enableBtIntent =newIntent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent,1);
}
C    搜索已配对设备,如果有则添加到配对列表中
//4.得到已经配对的所有蓝牙设备
Set<BluetoothDevice> bondedDevices = adapter.getBondedDevices();
if (bondedDevices.size()>0) {
for (Iterator iterator = bondedDevices.iterator();iterator.hasNext();) {
BluetoothDevice bluetoothDevice = (BluetoothDevice) iterator.next();
System.out.println(bluetoothDevice.getAddress());
}
}
D    搜索未配对设备,添加到未配对列表
//设置过滤器,接收蓝牙扫描到的信息
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
broadcastReceiver = new BluetoothReceiver();
//注册该广播
registerReceiver(broadcastReceiver, filter);
    private class BluetoothReceiver extends BroadcastReceiver{
 
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
//获取到扫描到的远程蓝牙设备信息
final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
listBluetoothName.add(device.getName());
listBluetoothDevice.add(device);
listAdapter.notifyDataSetChanged();
}
}
}
protected void onDestroy() {
super.onDestroy();
//记得关闭蓝牙
unregisterReceiver(broadcastReceiver);
}
E    如果是服务器端,需要建立监听,注意监听的是某个服务的UUID,服务器监听类如下:
private class AcceptThread extends Thread {private final BluetoothServerSocket mmServerSocket;public AcceptThread() {BluetoothServerSocket tmp = null;try {tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord("blue",MY_UUID);} catch (IOException e) { }mmServerSocket = tmp;}public void run(){BluetoothSocket socket = null;while(true){try{socket = mmServerSocket.accept();}catch(IOException e){break;}if(socket!=null){ConnectedThread ced=new ConnectedThread(socket);//监听到则建立连接ced.start();}try{mmServerSocket.close();} catch(Exception e){break;}}public void cancle(){try{mmServerSocket.close();}catch (IOException e) { }}}
F 如果是客户端,则需要直接通过UUID连接服务端,客户端连接类如下:public class ConnectThread extends Thread {private final BluetoothSocket mmSocket;private final BluetoothDevice mmDevice;public ConnectThread(BluetoothDevice device) {// Use a temporary object that is later assigned to mmSocket,// because mmSocket is finalBluetoothSocket tmp = null;mmDevice = device;// Get a BluetoothSocket to connect with the given BluetoothDevicetry {// MY_UUID is the app's UUID string, also used by the server codetmp = device.createRfcommSocketToServiceRecord(MY_UUID);} catch (IOException e) { }mmSocket = tmp;}public void run() {// Cancel discovery because it will slow down the connectionmBluetoothAdapter.cancelDiscovery();try {// Connect the device through the socket. This will block// until it succeeds or throws an exceptionmmSocket.connect();ConnectedThread ced=new ConnectedThread(mmSocket);ced.start();} catch (Exception e) {Log.e("connect0e",e.toString());//t1.append("\r\n 连接失败001" );// Unable to connect; close the socket and get outtry {mmSocket.close();} catch (Exception e1){Log.e("close",e1.toString());}}// Do work to manage the connection (in a separate thread) //manageConnectedSocket(mmSocket);}/** Will cancel an in-progress connection, and close the socket */public void cancel() {try {mmSocket.close();} catch (IOException e) { }}}G 客户端与服务器端建立连接成功后,需要ConnectedThread类接收发送数据:private class ConnectedThread extends Thread {private final BluetoothSocket mmSocket;private final InputStream mmInStream;private final OutputStream mmOutStream;public ConnectedThread(BluetoothSocket socket) {mmSocket = socket;InputStream tmpIn = null;OutputStream tmpOut = null;// Get the input and output streams, using temp objects because// member streams are finaltry {tmpIn = socket.getInputStream();tmpOut = socket.getOutputStream();} catch (IOException e) { }mmInStream = tmpIn;mmOutStream = tmpOut;}public void run() {byte[] buffer = new byte[1024]; // buffer store for the streamint bytes; // bytes returned from read()// Keep listening to the InputStream until an exception occurswhile (true) {try {// Read from the InputStreambytes = mmInStream.read(buffer);// Send the obtained bytes to the UI activity// mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)//.sendToTarget();String str= new String(buffer,"ISO-8859-1");str=str.substring(0,bytes);Log.e("read",str);} catch (IOException e) {break;}}}/* Call this from the main activity to send data to the remote device */public void write(byte[] bytes) {try {mmOutStream.write(bytes);} catch (IOException e) { }}/* Call this from the main activity to shutdown the connection */public void cancel() {try {mmSocket.close();} catch (IOException e) { }}}   
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 经典蓝牙
相关文章推荐