Android手机蓝牙开发

Android手机与蓝牙模块通讯流程

大体流程:

Android手机一般以客户端的角色主动连接SPP协议设备(接上蓝牙模块的数字传感器),连接流程是:
1.使用registerReceiver注册BroadcastReceiver来获取蓝牙状态、搜索设备等消息;
2.使用BlueAdatper的搜索;
3.在BroadcastReceiver的onReceive()里取得搜索所得的蓝牙设备信息(如名称,MAC,RSSI);
4.通过设备的MAC地址来建立一个BluetoothDevice对象;
5.由BluetoothDevice衍生出BluetoothSocket,准备SOCKET来读写设备;
6.通过BluetoothSocket的createRfcommSocketToServiceRecord()方法来选择连接的协议/服务,这里用的是SPP(UUID:00001101-0000-1000-8000-00805F9B34FB);
7.Connect之后(如果还没配对则系统自动提示),使用BluetoothSocket的getInputStream()和getOutputStream()来读写蓝牙设备。

具体流程:

1、首先要添加蓝牙的权限,避免忘记

<uses-permissionandroid:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permissionandroid:name="android.permission.BLUETOOTH" />

2、Android支持在程序内打开蓝牙,首先,需要获得一个BluetoothAdapter,可以通过getDefaultAdapter()获得系统默认的蓝牙适配器,通过通过enable()方法打开蓝牙。也可以在onstart()方法中请求用户是否打开蓝牙,用法:

Intent openBTIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(openBTIntent, REQUESTCODE);

如果用户选择打开蓝牙,下次进来后就不会再提示用户打开,直接进入,如果用户选择不打开,下次进来后还会提示用户是否打开蓝牙。
3、打开蓝牙后,就要搜索蓝牙设备,startDiscovery()方法用户搜索蓝牙设备,这是一个很耗性能的操作,在扫描之前可以先使用getBondedDevices()获取已经配对过的设备(可直接连接),避免不必要的消耗。
4、开始搜索蓝牙设备后,可以在广播接收器中获取搜索到的蓝牙设备

privatefinalBroadcastReceiver mReceiver = newBroadcastReceiver() {
        @Override
        publicvoidonReceive(Context context, Intent intent) {
            String action = intent.getAction();


            // When discovery finds a device
            if(BluetoothDevice.ACTION_FOUND.equals(action))
            {
                // Get the BluetoothDevice object from the Intent
                // 通过EXTRA_DEVICE附加域来得到一个BluetoothDevice设备
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

                // If it's already paired, skip it, because it's been listed already
                // 如果这个设备是不曾配对过的,添加到list列表
                if(device.getBondState() != BluetoothDevice.BOND_BONDED)
                {
                    list.add(newChatMessage(device.getName() + "\n"+ device.getAddress(), false));
                    clientAdapter.notifyDataSetChanged();
                    mListView.setSelection(list.size() - 1);
                }
            // When discovery is finished, change the Activity title
            }
            elseif(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action))
            {
                setProgressBarIndeterminateVisibility(false);
                if(mListView.getCount() == 0)
                {
                    list.add(newChatMessage("没有发现蓝牙设备", false));
                    clientAdapter.notifyDataSetChanged();
                    mListView.setSelection(list.size() - 1);
                }
            }
        }
    };

广播接收器要进行注册

IntentFilter filter = newIntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);

5、搜索到蓝牙设备后就可以连接蓝牙设备,通过Mac地址发送连接请求,在这之前必须使用cancelDiscovery()方法停止扫描。

mBluetoothAdapter.cancelDiscovery();
// 通过Mac地址去尝试连接一个设备
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(BluetoothMsg.BlueToothAddress);

6、通过UUID使两个设备之间建立连接。
客户端:主动发请求

BluetoothSocket socket = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));          
// 通过socket连接服务器,这是一个阻塞过程,直到连接建立或者连接失效
socket.connect();

服务端:接受一个请求

BluetoothServerSocket mServerSocket = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(PROTOCOL_SCHEME_RFCOMM, UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));       
/* 接受客户端的连接请求 */
// 这是一个阻塞过程,直到建立一个连接或者连接失效
// 通过BluetoothServerSocket得到一个BluetoothSocket对象,管理这个连接
BluetoothSocket socket = mServerSocket.accept();

7、 通过InputStream/outputStream读写数据流,已达到通信目的。

OutputStream os = socket.getOutputStream();
os.write(msg.getBytes());
InputStream is = null;
try{
    is = socket.getInputStream();
} catch(IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

8、关闭所有线程以及socket,取消注册广播,并关闭蓝牙设备

       if(mClientThread != null) {
    mClientThread.interrupt();
    mClientThread = null;
}
if(mReadThread != null) {
    mReadThread.interrupt();
    mReadThread = null;
}
try{
    if(socket != null) {
        socket.close();
        socket = null;
    }
} catch(IOException e) {
    // TODO: handle exception
}
  if(mBluetoothAdapter != null) {
    mBluetoothAdapter.cancelDiscovery();
    // 关闭蓝牙
    mBluetoothAdapter.disable();
}
unregisterReceiver(mReceiver);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值