Android蓝牙应用程序开发的基本步骤
来源:互联网 发布:异星工厂物流网络 编辑:程序博客网 时间:2024/05/02 04:28
在Android系统中,开发蓝牙应用程序的基本步骤如下:
1.设置权限
在文件AndroidManifest.xml中声明使用蓝牙的权限,代码如下:
<uses-permission android:name = "android.permission.BLUETOOTH"/><uses-permission android:name = "andriod.permission.BLUETOOTH_ADMIN"/>
2.启动蓝牙
BluetoothAdapter mBuletoothAdapter = BuletoothAdapter.getDefaultAdapter();if(mBuletoothAdapter == 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)首先使本机蓝牙处于可见状态(即处于易被搜索到的状态),便于其他设备发现本机蓝牙,演示代码如下:
//使本机蓝牙在300秒内可被搜索private void ensureDiscoverable() { if(mBuletoothAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) { Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DRATION, 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){ //device.getName() +" "+ device.getAddress()); }} else { mPairedDeviceArrayAdapter.add("没有找到已配对的设备");}
(3)最后通过 mBluetoothAdapter.startDiscovery() 方法来搜索设备,在此需要注册一个 BroadcastReceiver 来获得这个搜索结果。即先注册再获取信息,然后进行处理。演示代码如下:
//注册,当一个设备被发现时调用onReceiveIntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); this.registerReceiver(mReceiver, filter);//当搜索结束后调用onReceivefilter = 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极为相似,此线程在准备连接之前启动。演示代码如下:
//UUID可以看做一个端口号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){ //此时可以新建一个数据交换线程,把此socket传进去 } } //取消监听 public void cancel() { try{ serverSocket.close(); } catch (IOException e){ Log.e("app", "Scoket Type", + socketType + "close() of server failed", e); } } }
5.交换数据
当搜索到蓝牙设备后,接下来可以获取设备的地址,通过此地址获取一个BluetoothDevice对象,可以将其看做一个客户端,通过对象device.createRfcommSocketTo ServiceRecord(MY_UUID)同一个UUID可与服务器建立连接获取另一个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); } connectionFailed(); //连接失败 return; } //此时可以新建一个数据交换线程,把此socket传进去 } public void cancel() { try{ socket.close(); } catch (IOException e){ Log.e("app", "close() of connect socket failed", e); } }}
6.建立数据通信线程
这一阶段的任务是读取通信数据,演示代码如下:
//建立连接后,进行数据通信的线程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); //把读取到的数据发送给UI进行显示 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 cancle() { try{ socket.close(); } catch (IOException e){ Log.e("app", "close() of connect socket failed", e); } }}
0 0
- Android蓝牙应用程序开发的基本步骤
- BLE蓝牙开发的基本步骤
- 开发JDBC应用程序的基本步骤
- Android蓝牙开发——蓝牙操作的步骤
- android开发之蓝牙的基本操作
- Android蓝牙BLE开发步骤
- Android开发-蓝牙-步骤方法
- 开发Struts2应用程序-基本步骤
- Android 蓝牙开发基本流程
- Android 蓝牙开发基本流程
- Android 蓝牙开发基本流程
- Android 蓝牙开发基本流程
- Android 蓝牙开发基本流程
- Android 蓝牙开发基本流程
- Android 蓝牙开发基本流程
- Android 蓝牙开发基本流程
- Android 蓝牙开发基本流程
- Android 蓝牙开发基本流程
- 求100以内的质数
- matlab求全相应
- RPM 使用
- 使用Java添加系统右键菜单--详解版
- poj2251 Dungeon Master
- Android蓝牙应用程序开发的基本步骤
- 扑克牌顺子
- KMP【模板】
- Java开发中的23种设计模式详解(转)
- 用Gson和SharedPreferences轻松构建android本地数据缓存
- 第三次C#作业-Excel数据读取及HTML文件初步
- TASK 重新加载宿主
- Unity3D研究之2D游戏开发制作原理
- <<诫子书>> 诸葛亮