日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

实现Android和PC之间的蓝牙通信

發(fā)布時(shí)間:2025/1/21 Android 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 实现Android和PC之间的蓝牙通信 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

?????? 這兩天想實(shí)現(xiàn)PC和安卓手機(jī)的通信,限于水平,知道的方法大概有兩種:基于數(shù)據(jù)包的socket和藍(lán)牙。雖然看起來簡單,但調(diào)也調(diào)了兩天多。自己測試了下socket,在室內(nèi)WIFI環(huán)境下時(shí)延大概是0.1s。而在3G網(wǎng)絡(luò)下時(shí)延居然達(dá)3s之多,而且只要不發(fā)數(shù)據(jù),端口貌似就會斷掉,總之,很不爽。于是,便考慮了藍(lán)牙的方法。

  實(shí)現(xiàn)手機(jī)和PC的藍(lán)牙通信,一種是最常用的藍(lán)牙虛擬串口,這種方法可以通過配置非常簡單地實(shí)現(xiàn),很多外置藍(lán)牙GPS都用這種做法。但大名鼎鼎的安卓卻不支持,因此對大部分外置GPS都不提供支持(可能安卓手機(jī)大部分包含內(nèi)置GPS,覺得外置的太雞肋了)。因此必須采用第二種,藍(lán)牙socket。

????? 在電腦上,實(shí)在不想去在C++下開發(fā),于是便尋找.NET組件,但實(shí)際上微軟的NET庫中不支持藍(lán)牙,因此必須采用第三方的控件,名字叫inthehand.

????? 這篇文章中詳細(xì)的介紹了inthehan

d組件,http://www.cnblogs.com/procoder/archive/2009/09/22/1571580.html。由于它討論了實(shí)現(xiàn)文件傳輸?shù)乃悸?#xff0c;我們在這篇文章中就只討論簡單的字符傳輸。

???????它的官方網(wǎng)站是inthehand.net,其中多數(shù)的類庫和方法都能找到。

 下面是手機(jī)端的初始化代碼。其中的具體含義可參照http://android.tgbus.com/Android/tutorial/201103/346657.shtml。

private PrintStream mPrintStream = null;
private BufferedReader mBufferedReader = null;

BluetoothAdapter myBluetoothAdapter
= null;
BluetoothServerSocket mBThServer
= null;
BluetoothSocket mBTHSocket
= null;



myBluetoothAdapter
= BluetoothAdapter.getDefaultAdapter();

myBluetoothAdapter.enable();
//open bth

Intent discoverableIntent
= new Intent(
BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);//使得藍(lán)牙處于可發(fā)現(xiàn)模式,持續(xù)時(shí)間150s
discoverableIntent.putExtra(
BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,
150);

 下面是PC上的初始化核心代碼:PC是作為客戶端出現(xiàn)的。它需要通過搜索獲取手機(jī)的藍(lán)牙MAC地址,實(shí)現(xiàn)通信。GUID又名UUID,是標(biāo)記硬件地址的一種方法。

?? /// <summary>
??????? /// 打開端口
??????? /// </summary>
??????? /// <param name="Name">端口名稱</param>
??????? /// <returns>成功否</returns>
??????? public bool OpenPort(string Name)
??????? {

??????????? InTheHand.Net.Bluetooth.BluetoothRadio.PrimaryRadio.Mode = InTheHand.Net.Bluetooth.RadioMode.Connectable;
??????????? InTheHand.Net.Sockets.BluetoothClient cli = new InTheHand.Net.Sockets.BluetoothClient();
??????????? InTheHand.Net.Sockets.BluetoothDeviceInfo[] devices = cli.DiscoverDevices();
??????????? foreach (InTheHand.Net.Sockets.BluetoothDeviceInfo device in devices)//設(shè)備搜尋
??????????? {
??????????????? device.Update();
??????????????? device.Refresh();
??????????????? MessageBox.Show("設(shè)備已找到");
??????????????? break;
??????????? }

?


??????????? BluetoothDeviceInfo bd = new BluetoothDeviceInfo(devices[0].DeviceAddress);
??????????? bluetoothClient = new BluetoothClient();


??????????? Guid mGUID = Guid.Parse("fa87c0d0-afac-11de-8a39-0800200c9a66");
??????????? bluetoothClient.Connect(devices[0].DeviceAddress, mGUID);//客戶端對地址實(shí)現(xiàn)連接,這是一個(gè)阻塞線程,需要服務(wù)器端的回應(yīng)

??????????? ReceiveThread = new Thread(ReceiveMethod);
??????????? ReceiveThread.Start();

?

??????????? return true;
??????? }

????????下面是手機(jī)接受PC端連接請求的方法:

View Code 1 if (connected)
2 {
3 return;
4 }
5 try
6 {
7 mBThServer = myBluetoothAdapter
8 .listenUsingRfcommWithServiceRecord(NAME_SECURE,
9 MY_UUID_SECURE);
10 } catch (IOException e)
11 {
12 // TODO Auto-generated catch block
13 e.printStackTrace();
14 }
15
16 try
17 {
18 mBTHSocket = mBThServer.accept();
19 } catch (IOException e)
20 {
21 // TODO Auto-generated catch block
22 e.printStackTrace();
23 }
24 try
25 {
26 mBufferedReader = new BufferedReader(new InputStreamReader(
27 mBTHSocket.getInputStream()));
28 } catch (IOException e1)
29 {
30 // TODO Auto-generated catch block
31 e1.printStackTrace();
32 }// 取得輸入、輸出流
33 try
34 {
35 mPrintStream = new PrintStream(
36 mBTHSocket.getOutputStream(), true);
37 connected = true;
38 } catch (IOException e)
39 {
40 // TODO Auto-generated catch block
41 e.printStackTrace();
42 }

?? 要通過手機(jī)發(fā)送數(shù)據(jù),執(zhí)行以下代碼即可:

mPrintStream.write(buff);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
// 發(fā)送給服務(wù)器
mPrintStream.flush();

????PC端的接受代碼:

while (isConnecting)
{

try
{
Stream peerStream
= bluetoothClient.GetStream();
peerStream.Read(buffer,
0, PACKETLENGTH);
//dataprocess();
}


catch (Exception ex)
{
isConnecting
= false;
MessageBox.Show(ex.ToString());

}

  這里要注意以下幾個(gè)小問題,但也就是這幾個(gè)問題,耽誤了我很久時(shí)間:

????? inthehand.net.personal是PC端上一定要用得到的庫,但注意這個(gè)庫函數(shù)的版本,我一開始用了的dll是600K左右的,編譯沒問題,運(yùn)行就會報(bào)錯(cuò),提示找不到dll。但后來左思右想,才發(fā)現(xiàn)還有另外的一個(gè)同名dll,150K左右,換過來以后一切OK,太坑爹了!

??????? 手機(jī)設(shè)備的藍(lán)牙硬件權(quán)限要打開,否則就沒法運(yùn)行。

??????? 還有我特想將手機(jī)做個(gè)藍(lán)牙HID設(shè)備,但這樣貌似是不行的。因?yàn)檫@個(gè)庫本身提供的方法不夠底層...總之還想再研究下。

??????? 有任何問題歡迎討論

????????

??????

總結(jié)

以上是生活随笔為你收集整理的实现Android和PC之间的蓝牙通信的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。