蓝牙通讯;与LAN一样,通过MAC地址来识别远程设备,
通过蓝牙串口协议(SPP)建立端口后以输入输出方式通讯,与socket通讯类似。关键代码在于:将建立,连接好的socket传入线程中,利用新开启的线程发送接收数据。
private static Context context;
private BluetoothAdapter m_bluetoothadapter;
private OutputStream outstream;
private InputStream instream;
private OnPortListener onportlistener;
// 构造方法
public BluetoothChat(Context pContext) {
context = pContext;
message.what = 1;
onportlistener = (OnPortListener) pContext;
}
public interface OnPortListener {
public abstract void OnReceiveData(String p_Message);
}
private Handler m_handler=new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
switch(msg.what)
{
case 1:
Toast.makeText(context, "蓝牙设备未开启", Toast.LENGTH_LONG);
}
}
};
public class PortListenThread extends Thread{
private BluetoothServerSocket serversocket;
BluetoothSocket clientsocket=null;
PortListenThread(BluetoothServerSocket socket)
{
serversocket=socket;
}
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
try {
//接受对方数据
clientsocket=serversocket.accept();
//获得输出流
outstream =clientsocket.getOutputStream();
//修改状态,改为连接状态
m_state=m_stateConnected;
while(m_state == m_stateConnected)
{
instream=clientsocket.getInputStream();
RecviceData();
}
} catch (IOException e) {
// TODO Auto-generated catch block
if(!m_bluetoothadapter.isEnabled())
{
m_handler.sendMessage(msg);
}
e.printStackTrace();
}
}
private void RecviceData() {
// TODO Auto-generated method stub
//高效接受数据:
byte[] bytes=new byte[8];//前8位为数据长度。
try {
instream.read(bytes);
String lenstr=new String(bytes);
int length=Integer.parseInt(lenstr);//长度
byte[] bts =new byte[length];
instream.read(bts);
String msg=lenstr+ new String(bts);
} catch (IOException e) {
// TODO Auto-generated catch block
Clone();//关闭socket ,重启
e.printStackTrace();
}//读取输入存在bytes
//调用回调,在界面处理
}
private void Clone() {
// TODO Auto-generated method stub
serversocket.close();
clientsocket.close();
if(instream!=null)
{
instream.close();
}
if(outstream!=null)
{
outstream.close();
}
}
private void SendData(String data)
{//发送数据
try {// 调用输出流向对方发送数据
outstream.write(data.getBytes());
outstream.flush();// 强制输出所有缓冲的数据
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private static final String S_NAME = "BluetoothChat";
private static final UUID S_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");// UUID随机产生
public void CreatePortListen()
{
//获取本机的一个蓝牙设备
m_bluetoothadapter =BluetoothAdapter.getDefaultAdapter();
if(m_bluetoothadapter==null || m_bluetoothadapter.isEnabled())
{
//建立一个无线设备监听端口 类似于socket通讯。
BluetoothServerSocket bluetoothst = null;
try {
bluetoothst = m_bluetoothadapter.listenUsingRfcommWithServiceRecord(S_NAME, S_UUID);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//启动线程监听:
PortListenThread thread=new PortListenThread(bluetoothst);
thread.start();
}
else
{
m_handler.sendMessage(msg);
}
}
注意 在最后,要XML开启 使用蓝牙权限。
上面OnPortListener 接口的使用目的在于可以让activity 实现这个接口,将数据传达到activity 显示。