導航:首頁 > 源碼編譯 > 手機應用源碼藍牙

手機應用源碼藍牙

發布時間:2022-07-11 16:57:42

㈠ 如何實現android藍牙開發 自動配對連接,並不彈出提示框

源碼 BluetoothDevice 類中還有兩個隱藏方法 cancelBondProcess()和cancelPairingUserInput() 這兩個方法一個是取消配對進程一個是取消用戶輸入 下面是自動配對的代碼 Mainfest,xml注冊 1 <</code>receiver android:name="." > 2 <</code>intent-filter> 3 <</code>action android:name="android.bluetooth.device.action.PAIRING_REQUEST" /> 4 </</code>intent-filter> 5 </</code>receiver> 自己在收到廣播時處理並將預先輸入的密碼設置進去 01 public class extends BroadcastReceiver 02 { 03 04 String strPsw = "0"; 05 06 @Override 07 public void onReceive(Context context, Intent intent) 08 { 09 // TODO Auto-generated method stub 10 if (intent.getAction().equals( 11 "android.bluetooth.device.action.PAIRING_REQUEST")) 12 { 13 BluetoothDevice btDevice = intent 14 .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 15 16 // byte[] pinBytes = BluetoothDevice.convertPinToBytes("1234"); 17 // device.setPin(pinBytes); 18 Log.i("tag11111", "ddd"); 19 try 20 { 21 ClsUtils.setPin(btDevice.getClass(), btDevice, strPsw); // 手機和藍牙採集器配對 22 ClsUtils.createBond(btDevice.getClass(), btDevice); 23 ClsUtils.cancelPairingUserInput(btDevice.getClass(), btDevice); 24 } 25 catch (Exception e) 26 { 27 // TODO Auto-generated catch block 28 e.printStackTrace(); 29 } 30 } 31 32 33 } 34 } 001 002 import java.lang.reflect.Field; 003 import java.lang.reflect.Method; 004 005 import android.bluetooth.BluetoothDevice; 006 import android.util.Log; 007 public class ClsUtils 008 { 009 010 014 static public boolean createBond(Class btClass, BluetoothDevice btDevice) 015 throws Exception 016 { 017 Method createBondMethod = btClass.getMethod("createBond"); 018 Boolean returnValue = (Boolean) createBondMethod.invoke(btDevice); 019 return returnValue.booleanValue(); 020 } 021 022 026 static public boolean removeBond(Class btClass, BluetoothDevice btDevice) 027 throws Exception 028 { 029 Method removeBondMethod = btClass.getMethod("removeBond"); 030 Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice); 031 return returnValue.booleanValue(); 032 } 033 034 static public boolean setPin(Class btClass, BluetoothDevice btDevice, 035 String str) throws Exception 036 { 037 try 038 { 039 Method removeBondMethod = btClass.getDeclaredMethod("setPin", 040 new Class[] 041 {byte[].class}); 042 Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice, 043 new Object[] 044 {str.getBytes()}); 045 Log.e("returnValue", "" + returnValue); 046 } 047 catch (SecurityException e) 048 { 049 // throw new RuntimeException(e.getMessage()); 050 e.printStackTrace(); 051 } 052 catch (IllegalArgumentException e) 053 { 054 // throw new RuntimeException(e.getMessage()); 055 e.printStackTrace(); 056 } 057 catch (Exception e) 058 { 059 // TODO Auto-generated catch block 060 e.printStackTrace(); 061 } 062 return true; 063 064 } 065 066 // 取消用戶輸入 067 static public boolean cancelPairingUserInput(Class btClass, 068 BluetoothDevice device) 069 070 throws Exception 071 { 072 Method createBondMethod = btClass.getMethod("cancelPairingUserInput"); 073 // cancelBondProcess() 074 Boolean returnValue = (Boolean) createBondMethod.invoke(device); 075 return returnValue.booleanValue(); 076 } 077 078 // 取消配對 079 static public boolean cancelBondProcess(Class btClass, 080 BluetoothDevice device) 081 082 throws Exception 083 { 084 Method createBondMethod = btClass.getMethod("cancelBondProcess"); 085 Boolean returnValue = (Boolean) createBondMethod.invoke(device); 086 return returnValue.booleanValue(); 087 } 088 089 093 static public void printAllInform(Class clsShow) 094 { 095 try 096 { 097 // 取得所有方法 098 Method[] hideMethod = clsShow.getMethods(); 099 int i = 0; 100 for (; i < hideMethod.length; i++) 101 { 102 Log.e("method name", hideMethod[i].getName() + ";and the i is:" 103 + i); 104 } 105 // 取得所有常量 106 Field[] allFields = clsShow.getFields(); 107 for (i = 0; i < allFields.length; i++) 108 { 109 Log.e("Field name", allFields[i].getName()); 110 } 111 } 112 catch (SecurityException e) 113 { 114 // throw new RuntimeException(e.getMessage()); 115 e.printStackTrace(); 116 } 117 catch (IllegalArgumentException e) 118 { 119 // throw new RuntimeException(e.getMessage()); 120 e.printStackTrace(); 121 } 122 catch (Exception e) 123 { 124 // TODO Auto-generated catch block 125 e.printStackTrace(); 126 } 127 } 128 } 執行時直接使用: 01 public static boolean pair(String strAddr, String strPsw) 02 { 03 boolean result = false; 04 BluetoothAdapter bluetoothAdapter = BluetoothAdapter 05 .getDefaultAdapter(); 06 07 bluetoothAdapter.cancelDiscovery(); 08 09 if (!bluetoothAdapter.isEnabled()) 10 { 11 bluetoothAdapter.enable(); 12 } 13 14 if (!BluetoothAdapter.checkBluetoothAddress(strAddr)) 15 { // 檢查藍牙地址是否有效 16 17 Log.d("mylog", "devAdd un effient!"); 18 } 19 20 BluetoothDevice device = bluetoothAdapter.getRemoteDevice(strAddr); 21 22 if (device.getBondState() != BluetoothDevice.BOND_BONDED) 23 { 24 try 25 { 26 Log.d("mylog", "NOT BOND_BONDED"); 27 ClsUtils.setPin(device.getClass(), device, strPsw); // 手機和藍牙採集器配對 28 ClsUtils.createBond(device.getClass(), device); 29 remoteDevice = device; // 配對完畢就把這個設備對象傳給全局的remoteDevice 30 result = true; 31 } 32 catch (Exception e) 33 { 34 // TODO Auto-generated catch block 35 36 Log.d("mylog", "setPiN failed!"); 37 e.printStackTrace(); 38 } // 39 40 } 41 else 42 { 43 Log.d("mylog", "HAS BOND_BONDED"); 44 try 45 { 46 ClsUtils.createBond(device.getClass(), device); 47 ClsUtils.setPin(device.getClass(), device, strPsw); // 手機和藍牙採集器配對 48 ClsUtils.createBond(device.getClass(), device); 49 remoteDevice = device; // 如果綁定成功,就直接把這個設備對象傳給全局的remoteDevice 50 result = true; 51 } 52 catch (Exception e) 53 { 54 // TODO Auto-generated catch block 55 Log.d("mylog", "setPiN failed!"); 56 e.printStackTrace(); 57 } 58 } 59 return result; 60 }

㈡ 求安卓Android手機上的的藍牙串口通訊源碼!!!!

http://code.eoe.cn/789?f_section=new
以上是地址 自己去下

㈢ android_studio手機藍牙串口通信源代碼

初涉android的藍牙操作,按照固定MAC地址連接獲取Device時,程序始終是異常終止,查了好多天代碼都沒查出原因。今天改了一下API版本,突然就成功連接了。總結之後發現果然是個坑爹之極的錯誤。

為了這種錯誤拚命查原因浪費大把時間是非常不值得的,但是問題不解決更是揪心。可惜我網路了那麼多,都沒有給出確切原因。今天特此mark,希望後來者遇到這個問題的時候能輕松解決。

下面是我的連接過程,中間崩潰原因及解決辦法。

1:用AT指令獲得藍牙串口的MAC地址,地址是簡寫的,按照常理猜測可得標准格式。

2:開一個String adress= "************" //MAC地址, String MY_UUID= "************"//UUID根據通信而定,網上都有。

3:取得本地Adapter用getDefaultAdapter(); 遠程的則用getRemoteDevice(adress); 之後便可用UUID開socket進行通信。

如果中途各種在getRemoteDevice處崩潰,大家可以查看一下當前的API版本,如果是2.1或以下版本的話,便能確定是API版本問題,只要換成2.2或者以上就都可以正常運行了~ 這么坑爹的錯誤的確很為難初學者。 唉·········· 為這種小trick浪費很多時間真是難過。

(另外有個重要地方,別忘了給manifest裡面加以下兩個藍牙操作許可權哦~)

㈣ 誰有android藍牙與電腦藍牙之間的通信源碼啊,或者只要一端裝了apk也行。

官方sdk自帶一個藍牙例子,自己找吧,名字就叫bluetooth

㈤ 誰能給一份開發基於藍牙4.0的android的demo源碼

請參考如下WIKI頁面,基本上目前有提供的BLE的參考代碼都集中到這個頁面了:http://processors.wiki.ti.com/index.php/Category:BluetoothLE

㈥ 怎麼將手機中的應用軟體用藍牙分享到另一手機

1、以華為P10為例,在桌面選擇需要分享的軟體;

㈦ 手機端應用怎麼和藍牙連接

手機藍牙常用於與藍牙耳機連接,以免提接聽拔打電話或聽音樂。以下是連接步驟:
手機要先打開藍牙並處在被發現狀態,按住藍牙耳機開關鍵不放,直到手機藍牙搜索到耳機並顯示出耳機型號,再點擊手機中顯示的耳機型號進行連接(有時還需要輸入PIN碼,默認一般是」1234"或「0000」),顯示「已連接」說明連接成功,此時方可松開耳機開關鍵。

      藍牙連接的有效距離是10米以內,使用過程中一定保持在這個距離范圍內,超出范圍會暫時斷開連接。

㈧ 關於android系統藍牙都用什麼模塊,有單獨的android驅動嗎

Android系統的源碼中藍牙部分的分布式這樣的:
1. App部分主要包括OPP和PBAP以及HFP,當然還有settings部分,代碼分布如下:
(1) /packages/app/Bluetooth/src/com/android/bluetooth/opp
(2) /packages/app/Bluetooth/src/com/android/bluetooth/pbap
(3) /packages/app/Phone/src/com/android/phone/ 中和BT相關的部分,主要是HFHS打電話相關的
(4) /packages/app/Settings/src/com/android/settings/bluetooth/ 主要是藍牙開啟,搜索,配對等

2. Framework部分主要包括BluetoothAdapter,BluetoothService,BluetoothEventLoop,BluetoothA2dpService等核心類,代碼分布如下:
(1)/frameworks/base/core/java/android/server/ 包含BluetoothService,BluetoothEventLoop等核心類,但是並沒有向用戶應用程序提供介面,屬於系統的類。
(2)/frameworks/base/core/java/android/bluetooth 包含了BluetoothAdapter, BluetoothSocket, BluetoothServerSocket等一系列類,這些類是系統向應用程序提供的介面,編寫藍牙相關的應用程序時會用到這些類,檢查BT是否打開,搜索設備等等。如果編寫藍牙應用程序,需要熟悉該部分類的功能。
(3) /frameworks/base/core/jni/ 中包含Bluetooth相關的文件,如 android_server_BluetoothService.cpp 該部分主要是java層和c/c++層進行通信的

3. Bluez部分,主要包括各個協議的實現,如A2dp,AVRCP,AVDTP,HID,HDP,PAN,DUN以及對BT host的實現,通過該部分向BT Controler部分發送HCI命令,代碼分布服下:
/external/bluetooth/bluez

4. BluetDroid部分,主要用於開關藍牙,晶元上電部分(不屬於驅動,可以算是硬體抽象層(HAL)), 代碼分布如下:
/system/bluetooth/

5. kernel部分,主要包括RFCOMM,L2CAP等協議以及HCI的實現,代碼分布如下:
/kernel/net/bluetooth/

6. driver部分,該部分代碼還真沒了解過,代碼分布:
/kernel/drivers/bluetooth/
另外,driver部分和具體晶元密切相關,有時在下面的目錄下也會有:
/vendor/
/device/

閱讀全文

與手機應用源碼藍牙相關的資料

熱點內容
噴油螺桿製冷壓縮機 瀏覽:578
python員工信息登記表 瀏覽:376
高中美術pdf 瀏覽:160
java實現排列 瀏覽:512
javavector的用法 瀏覽:981
osi實現加密的三層 瀏覽:231
大眾寶來原廠中控如何安裝app 瀏覽:913
linux內核根文件系統 瀏覽:242
3d的命令面板不見了 瀏覽:525
武漢理工大學伺服器ip地址 瀏覽:148
亞馬遜雲伺服器登錄 瀏覽:524
安卓手機如何進行文件處理 瀏覽:70
mysql執行系統命令 瀏覽:929
php支持curlhttps 瀏覽:142
新預演算法責任 瀏覽:443
伺服器如何處理5萬人同時在線 瀏覽:250
哈夫曼編碼數據壓縮 瀏覽:424
鎖定伺服器是什麼意思 瀏覽:383
場景檢測演算法 瀏覽:616
解壓手機軟體觸屏 瀏覽:348