标签:request 任务 弹出对话框 close 添加 comm device putextra instr
本文转载自:http://blog.csdn.net/vnanyesheshou/article/details/51554852
随着可穿戴设备的流行,研究蓝牙是必不可少的一门技术了。
总结了下蓝牙开发使用的一些东西分享一下。
首先需要AndroidManifest.xml文件中添加操作蓝牙的权限。
<uses-permissionandroid:name="Android.permission.BLUETOOTH" />
允许程序连接到已配对的蓝牙设备。
<uses-permissionandroid:name="android.permission.BLUETOOTH_ADMIN" />
允许程序发现和配对蓝牙设备。
操作蓝牙主要用到的类 BluetoothAdapter类,使用时导包
import android.bluetooth.BluetoothAdapter;
源码具体位置frameworks/base/core/Java/android/bluetooth/BluetoothAdapter.java
BluetoothAdapter 代表本地设备的蓝牙适配器。该BluetoothAdapter可以执行基本的蓝牙任务,例如启
动设备发现,查询配对的设备列表,使用已知的MAC地址实例化一个BluetoothDevice类,并创建一个
BluetoothServerSocket监听来自其他设备的连接请求。
获取蓝牙适配器
- BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
开启蓝牙
- if(!mBluetoothAdapter.isEnabled()){
- Intent enabler = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
- startActivityForResult(enabler, REQUEST_ENABLE);
-
-
- }
获取本地蓝牙信息
- String name = mBluetoothAdapter.getName();
- String address = mBluetoothAdapter.getAddress();
- Log.d(TAG,"bluetooth name ="+name+" address ="+address);
- Set<BluetoothDevice> devices = mBluetoothAdapter.getBondedDevices();
- Log.d(TAG, "bonded device size ="+devices.size());
- for(BluetoothDevice bonddevice:devices){
- Log.d(TAG, "bonded device name ="+bonddevice.getName()+" address"+bonddevice.getAddress());
- }
打印:
- D/MainActivity(16800): bluetooth name =Coolpad 8297 address =18:DC:56:D2:30:BD
- D/MainActivity(16800): bonded device size =1
- D/MainActivity(16800): bonded device name =小米手机 addressF8:A4:5F:02:B1:8F
搜索设备
mBluetoothAdapter.startDiscovery();
开始搜索设备,该过程是异步的,通过下面注册广播接受者,可以监听是否搜到设备。
- IntentFilter filter = new IntentFilter();
- filter.addAction(BluetoothDevice.ACTION_FOUND);
- filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
- filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
- registerReceiver(mBluetoothReceiver, filter);
监听扫描结果
- private BroadcastReceiver mBluetoothReceiver = new BroadcastReceiver(){
- @Override
- public void onReceive(Context context, Intent intent) {
- String action = intent.getAction();
- Log.d(TAG,"mBluetoothReceiver action ="+action);
- if(BluetoothDevice.ACTION_FOUND.equals(action)){
-
- BluetoothDevice scanDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
- if(scanDevice == null || scanDevice.getName() == null) return;
- Log.d(TAG, "name="+scanDevice.getName()+"address="+scanDevice.getAddress());
-
- String name = scanDevice.getName();
- if(name != null && name.equals(VnApplication.BLUETOOTH_NAME)){
- mBluetoothAdapter.cancelDiscovery();
-
- mProgressDialog.setTitle(getResources().getString(R.string.progress_connecting));
- mBlthChatUtil.connect(scanDevice);
- }
- }else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
-
- }
- }
-
- };
mBlthChatUtil.connect(scanDevice)连接到设备,
此方法中主要是获取socket客户端,并连接。
BluetoothSocket mmSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
mmSocket.connect();
当然有客户端就有服务端了,服务端应先开启,并一直等待客户端连接。直到连接成功。
- private class AcceptThread extends Thread {
-
- private final BluetoothServerSocket mServerSocket;
- public AcceptThread() {
- BluetoothServerSocket tmp = null;
-
- try {
- tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
- } catch (IOException e) {
- Log.e(TAG, "listen() failed", e);
- }
- mServerSocket = tmp;
- }
-
- public void run() {
- if (D) Log.d(TAG, "BEGIN mAcceptThread" + this);
- setName("AcceptThread");
- BluetoothSocket socket = null;
-
- while (mState != STATE_CONNECTED) {
- try {
-
-
- socket = mServerSocket.accept();
- } catch (IOException e) {
- Log.e(TAG, "accept() failed", e);
- break;
- }
-
- if (socket != null) {
- synchronized (BluetoothChatUtil.this) {
- switch (mState) {
- case STATE_LISTEN:
- case STATE_CONNECTING:
-
- connected(socket, socket.getRemoteDevice());
- break;
- case STATE_NONE:
- case STATE_CONNECTED:
-
- try {
- socket.close();
- } catch (IOException e) {
- Log.e(TAG, "Could not close unwanted socket", e);
- }
- break;
- }
- }
- }
- }
- if (D) Log.i(TAG, "END mAcceptThread");
- }
- }
当连接成功后,connected(mmSocket, mmDevice); 获取输入输出流,从而进行通信。
private InputStream mmInStream = socket.getInputStream();
private OutputStream mmOutStream =socket.getOutputStream();
mmOutStream.write(buffer);发送信息。
mmInStream.read(buffer); 收到消息。
有时候扫描不到设备,需要设备蓝牙可见,这样才能被搜索到。
设置蓝牙可见性
- Intent discoveryIntent = new Intent(
- BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
- discoveryIntent.putExtra(
- BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
- startActivity(discoveryIntent);
demo下载
Android蓝牙通讯【转】
标签:request 任务 弹出对话框 close 添加 comm device putextra instr
原文地址:http://www.cnblogs.com/zzb-Dream-90Time/p/6625372.html