标签:android http java os 数据 io
蓝牙是一种支持设备短距离传输数据的无线技术。android在2.0以后提供了这方面的支持。
从查找蓝牙设备到能够相互通信要经过几个基本步骤(本机做为服务器):
1.设置权限
在manifest中配置
- <uses-permission android:name="android.permission.BLUETOOTH"/>
- <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
2.启动蓝牙
首先要查看本机是否支持蓝牙,获取BluetoothAdapter蓝牙适配器对象
- BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
- if(mBluetoothAdapter == null){
-
- return;
- }
- if(!mBluetoothAdapter.isEnabled()){
- Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
- startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
- }
- public void onActivityResult(int requestCode, int resultCode, Intent data){
- if(requestCode == REQUEST_ENABLE_BT){
- if(requestCode == RESULT_OK){
-
- }
- }
- }
3。发现蓝牙设备
这里可以细分为几个方面
(1)使本机蓝牙处于可见(即处于易被搜索到状态),便于其他设备发现本机蓝牙
- private void ensureDiscoverable() {
- if (mBluetoothAdapter.getScanMode() !=
- BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
- Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
- discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
- startActivity(discoverableIntent);
- }
- }
(2)查找已经配对的蓝牙设备,即以前已经配对过的设备
- Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
- if (pairedDevices.size() > 0) {
- findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);
- for (BluetoothDevice device : pairedDevices) {
-
- }
- } else {
- mPairedDevicesArrayAdapter.add("没有找到已匹对的设备");
- }
(3)通过mBluetoothAdapter.startDiscovery();搜索设备,要获得此搜索的结果需要注册
一个BroadcastReceiver来获取。先注册再获取信息,然后处理
- IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
- this.registerReceiver(mReceiver, filter);
-
- filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
- this.registerReceiver(mReceiver, filter);
- private BroadcastReceiver mReceiver = 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);
-
- if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
- mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
- }
- }else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
- if (mNewDevicesArrayAdapter.getCount() == 0) {
- mNewDevicesArrayAdapter.add("没有搜索到设备");
- }
- }
-
- }
- };
4.建立连接
查找到设备 后,则需要建立本机与其他设备之间的连接。
一般用本机搜索其他蓝牙设备时,本机可以作为一个服务端,接收其他设备的连接。
启动一个服务器端的线程,死循环等待客户端的连接,这与ServerSocket极为相似。
这个线程在准备连接之前启动
- private static final UUID MY_UUID =
- UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66");
-
- private class AcceptThread extends Thread{
- private BluetoothServerSocket serverSocket;
-
- public AcceptThread(boolean secure){
- BluetoothServerSocket temp = null;
- try {
- temp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(
- NAME_INSECURE, MY_UUID);
- } catch (IOException e) {
- Log.e("app", "listen() failed", e);
- }
- serverSocket = temp;
- }
-
- public void run(){
- BluetoothSocket socket=null;
- while(true){
- try {
- socket = serverSocket.accept();
- } catch (IOException e) {
- Log.e("app", "accept() failed", e);
- break;
- }
- }
- if(socket!=null){
-
- }
- }
-
-
- public void cancel(){
- try {
- serverSocket.close();
- } catch (IOException e) {
- Log.e("app", "Socket Type" + socketType + "close() of server failed", e);
- }
- }
-
- }
搜索到设备后可以获取设备的地址,通过此地址获取一个BluetoothDeviced对象,可以看做客户端,通过此对象device.createRfcommSocketToServiceRecord(MY_UUID);同一个UUID可与服务器建立连接获取另一个socket对象,由此服务端与客户端各有一个socket对象,此时
他们可以互相交换数据了。
创立客户端socket可建立线程
-
- private class ConnectThread extends Thread{
- private BluetoothSocket socket;
- private BluetoothDevice device;
- public ConnectThread(BluetoothDevice device,boolean secure){
- this.device = device;
- BluetoothSocket tmp = null;
- try {
- tmp = device.createRfcommSocketToServiceRecord(MY_UUID_SECURE);
- } catch (IOException e) {
- Log.e("app", "create() failed", e);
- }
- }
-
- public void run(){
- mBluetoothAdapter.cancelDiscovery();
- try {
- socket.connect();
- } catch (IOException e) {
- try {
- socket.close();
- } catch (IOException e1) {
- Log.e("app", "unable to close() "+
- " socket during connection failure", e1);
- }
- connetionFailed();
- return;
- }
-
- }
-
- public void cancel() {
- try {
- socket.close();
- } catch (IOException e) {
- Log.e("app", "close() of connect socket failed", e);
- }
- }
- }
5.建立数据通信线程,进行读取数据
- private class ConnectedThread extends Thread{
- private BluetoothSocket socket;
- private InputStream inStream;
- private OutputStream outStream;
-
- public ConnectedThread(BluetoothSocket socket){
-
- this.socket = socket;
- try {
-
- inStream = socket.getInputStream();
- outStream = socket.getOutputStream();
- } catch (IOException e) {
- Log.e("app", "temp sockets not created", e);
- }
- }
-
- public void run(){
- byte[] buff = new byte[1024];
- int len=0;
-
- while(true){
- try {
- len = inStream.read(buff);
-
- Message msg = handler.obtainMessage(BluetoothChat.MESSAGE_READ,
- len, -1, buff);
- msg.sendToTarget();
- } catch (IOException e) {
- Log.e("app", "disconnected", e);
- connectionLost();
- start();
- break;
- }
- }
- }
-
-
- public void write(byte[] buffer) {
- try {
- outStream.write(buffer);
-
-
- handler.obtainMessage(BluetoothChat.MESSAGE_WRITE, -1, -1, buffer)
- .sendToTarget();
- } catch (IOException e) {
- Log.e("app", "Exception during write", e);
- }
- }
-
- public void cancel() {
- try {
- socket.close();
- } catch (IOException e) {
- Log.e("app", "close() of connect socket failed", e);
- }
- }
- }
到这里,蓝牙通信的基本操作已经全部完成。
Android蓝牙操作笔记,布布扣,bubuko.com
Android蓝牙操作笔记
标签:android http java os 数据 io
原文地址:http://www.cnblogs.com/xiaochao1234/p/3849150.html