1.Androidå¼åä¹èçï¼Bluetoothï¼
2.android bluedroid是源牙cyclibarriar源码什么设备
3.如何使用android原生BLE蓝牙进行操作?
4.Android èçå¼å(ä¸)
5.Androidèçå¼åââå®ç°èçè天
6.Android èçå¼å(ä¸)-èçç详ç»ä»ç»
Androidå¼åä¹èçï¼Bluetoothï¼
å¨ä¸ä¸ç¯ä¸æä»ç»äºWifiä¸ç½ç»è¿æ¥å¤çAndroidå¼åä¹WiFiä¸ç½ç»è¿æ¥å¤ç
ä¸é¢ï¼æ¥ç»§ç»è¯´è¯´Androidä¸èççåºæ¬ä½¿ç¨ã
Bluetoothæ¯ç®å使ç¨çæ广æ³çæ 线é讯åè®®ä¹ä¸,主è¦é对çè·ç¦»è®¾å¤é讯ï¼ç±³ï¼ï¼å¸¸ç¨äºè¿æ¥è³æºãé¼ æ å移å¨é讯设å¤çã
å¼å¾ä¸æçæ¯ï¼
android4.2æ°å¢äºé¨åæ°åè½ï¼ä½æ¯å¯¹äºBluetoothçæç人æ许å¼å§å¤´ç¼äºï¼é£å°±æ¯Android4.2å¼å ¥äºä¸ä¸ªæ°çèçåè®®æ éBLEãè°·æåBroadcomä¹é´çåä½ï¼å¼åæ°çèçåè®®æ ï¼å代äºåºäºå æ çBluezãå æ¤å¸åºä¸åºç°äºè设å¤çå ¼å®¹é®é¢ï¼å¾å¤èç设å¤å¨android4.2ææºä¸ä¸è½æ£å¸¸ä½¿ç¨ã
BluetoothAdapterç®åç¹æ¥è¯´å°±æ¯ä»£è¡¨äºæ¬è®¾å¤(ææºãçµèç)çèçéé å¨å¯¹è±¡ã
firstï¼we need permission
è¦æä½èçï¼å è¦å¨AndroidManifest.xmléå å ¥æé
**ä¸é¢æ¥ççå¦ä½ä½¿ç¨èçã **âââ
****Demo已就绪ï¼
è¿åå¼ï¼å¦æ设å¤å ·å¤èçåè½ï¼è¿åBluetoothAdapter å®ä¾ï¼å¦åï¼è¿ånull对象ã
æå¼èç设å¤çæ¹å¼ï¼
1.ç´æ¥è°ç¨å½æ°enable()å»æå¼èçè®¾å¤ ï¼
2.ç³»ç»APIå»æå¼èç设å¤ï¼è¯¥æ¹å¼ä¼å¼¹åºä¸ä¸ªå¯¹è¯æ¡æ ·å¼çActivityä¾ç¨æ·éæ©æ¯å¦æå¼èç设å¤ã
注æï¼1.å¦æèçå·²ç»å¼å¯ï¼ä¸ä¼å¼¹åºè¯¥Activityçé¢ã2.å¨ç®å大å¤æ°Androidææºä¸ï¼æ¯ä¸æ¯æå¨é£è¡æ¨¡å¼ä¸å¼å¯èççãå¦æèçå·²ç»å¼å¯ï¼é£ä¹èççå¼å ³ ,ç¶æä¼éçé£è¡æ¨¡å¼çç¶æèåçæ¹åã
1. æç´¢èç设å¤
使ç¨BluetoothAdapterçstartDiscovery()æ¹æ³æ¥æç´¢èç设å¤
startDiscovery()æ¹æ³æ¯ä¸ä¸ªå¼æ¥æ¹æ³ï¼è°ç¨åä¼ç«å³è¿åã该æ¹æ³ä¼è¿è¡å¯¹å ¶ä»èç设å¤çæç´¢ï¼è¯¥è¿ç¨ä¼æç»ç§ã该æ¹æ³è°ç¨åï¼æç´¢è¿ç¨å®é ä¸æ¯å¨ä¸ä¸ªSystem Serviceä¸è¿è¡çï¼æ以å¯ä»¥è°ç¨cancelDiscovery()æ¹æ³æ¥åæ¢æç´¢ï¼è¯¥æ¹æ³å¯ä»¥å¨æªæ§è¡discovery请æ±æ¶è°ç¨ï¼ã
ç³»ç»å¼å§æç´¢èç设å¤
^( * ï¿£(oo)ï¿£ ) ^ ç³»ç»ä¼åé以ä¸ä¸ä¸ªå¹¿æï¼
2.æ«æ设å¤
3.å®ä¹å¹¿ææ¥æ¶å¨æ¥æ¶æç´¢ç»æ
4.注å广æ
è·åéè¿çèç设å¤
第ä¸æ¥å»ºç«è¿æ¥ï¼é¦å Android sdkï¼2.0以ä¸çæ¬ï¼æ¯æçèçè¿æ¥æ¯éè¿BluetoothSocket建ç«è¿æ¥ï¼æå¡ç«¯BluetoothServerSocketå客æ·ç«¯ï¼BluetoothSocketï¼éæå®åæ ·çUUIDï¼æè½å»ºç«è¿æ¥ï¼å 为建ç«è¿æ¥çæ¹æ³ä¼é»å¡çº¿ç¨ï¼æ以æå¡å¨ç«¯å客æ·ç«¯é½åºå¯å¨æ°çº¿ç¨è¿æ¥ã
ï¼è¿éçæå¡ç«¯å客æ·ç«¯æ¯ç¸å¯¹æ¥è¯´çï¼
两个èç设å¤ä¹é´çè¿æ¥ï¼åå¿ é¡»å®ç°æå¡ç«¯ä¸å®¢æ·ç«¯çæºå¶ã
å½ä¸¤ä¸ªè®¾å¤å¨åä¸ä¸ªRFCOMM channelä¸åå«æ¥æä¸ä¸ªè¿æ¥çBluetoothSocketï¼è¿ä¸¤ä¸ªè®¾å¤æå¯ä»¥è¯´æ¯å»ºç«äºè¿æ¥ã
æå¡ç«¯è®¾å¤ä¸å®¢æ·ç«¯è®¾å¤è·åBluetoothSocketçéå¾æ¯ä¸åçã
1ï¼æå¡ç«¯è®¾å¤æ¯éè¿acceptedä¸ä¸ªincoming connectionæ¥è·åçï¼
2ï¼å®¢æ·ç«¯è®¾å¤åæ¯éè¿æå¼ä¸ä¸ªå°æå¡ç«¯çRFCOMM channelæ¥è·åçã
æå¡ç«¯
éè¿è°ç¨BluetoothAdapterçlistenUsingRfcommWithServiceRecord(String, UUID)æ¹æ³æ¥è·åBluetoothServerSocketï¼UUIDç¨äºå®¢æ·ç«¯ä¸æå¡ç«¯ä¹é´çé 对ï¼
客æ·ç«¯
è°ç¨BluetoothServiceçcreateRfcommSocketToServiceRecord(UUID)æ¹æ³è·åBluetoothSocketï¼è¯¥UUIDåºè¯¥åäºæå¡ç«¯çUUIDï¼ã
è°ç¨BluetoothSocketçconnect()æ¹æ³ï¼è¯¥æ¹æ³ä¸ºblockæ¹æ³ï¼ï¼å¦æUUIDåæå¡ç«¯çUUIDå¹é ï¼å¹¶ä¸è¿æ¥è¢«æå¡ç«¯acceptï¼åconnect()æ¹æ³è¿åã
æ°æ®ä¼ éï¼éè¿ä»¥ä¸æä½ï¼å°±å·²ç»å»ºç«çBluetoothSocketè¿æ¥äºï¼æ°æ®ä¼ éæ éæ¯éè¿æµçå½¢å¼
è·åæµ
该类就æ¯å ³äºè¿ç¨èç设å¤çä¸ä¸ªæè¿°ãéè¿å®å¯ä»¥åæ¬å°èç设å¤---BluetoothAdapterè¿æ¥éä¿¡ã
好å¤ä¸è¥¿æä¹ä¸ç¥éæä¹æè¿°ï¼ä¸é¢ç»åºDemoï¼
å好æåå¦ä¹ çå°ä¼ä¼´é®æListViewæä¹ç¨ï¼é£æå°±ç¨ListViewã
æºç ï¼
RairDemo
GitHub: /Rairmmd/android-demo
Coding: /s?__biz=MzU5NzA2NjQzMg==&mid=&idx=1&sn=9db9adfbbcc&scene=#wechat_redirect
Androidèçå¼åââå®ç°èçè天
ä¸èçå¼å主è¦çç¸å ³ç±»æ¯ä»¥ä¸å个ç¥é对åºAPIåå°±å¯ä»¥è¿è¡å¯¹åºçèçå¼åï¼è¿é以è·åèç设å¤ä¸ºä¾å
}
æ索设å¤çåè°åéè¦éè¿æ³¨å广æçå½¢å¼æ¥è·å
å®ä¹å¹¿æ
ä¹åå°±å¯ä»¥è¿è¡ä¸ªäººçä¸äºæä½
è¦å®ç°èçè天åæ¶åå°èçä¹é´çä¼ è¾éä¿¡ï¼åé¢ä¹è¯´å°äºï¼è¿éè¯å®å°±æ¯ç¨å°BluetoothServerSocket以åBluetoothSocketã
èçä¼ è¾éä¿¡ç¸å½äºæå¡å¨ç«¯ä¸å®¢æ·ç«¯ä¹é´çéä¿¡ï¼åªä¸è¿ä¸åæ¯è¿éæ¯ä¸ä¸ªèç设å¤æ¬èº«èªå·±æ¢å å½æå¡å¨ç«¯ä¹å å½å®¢æ·ç«¯ï¼å¤§è´çå ³ç³»å°±æ¯
注æï¼è¿äºè¿æ¥é½æ¯é»å¡å¼çï¼é½è¦æ¾å¨çº¿ç¨éå»æ§è¡ã
å¯ä»¥çå°ï¼å½BluetoothServerSocketçå¬å°æ设å¤è¿æ¥çæ¶åï¼å°±ä¼è°ç¨dataTransferå¼å¯ä¸ä¸ªæ°æ®ä¼ è¾ã
éè¦ä¸ä¸ªConnectThreadæ¥åèµ·
ä¹å建ç«è¿æ¥ä¹åå°±ä¼è°ç¨dataTransferæ¥è¿è¡æ°æ®ä¼ è¾ï¼åæ ·ä¹éè¦ä¸ä¸ªçº¿ç¨æ¥ç»´æ¤æ°æ®ä¼ è¾
èçè天åæ¯åºäºä¸é¢ä¸ä¸ªçº¿ç¨æ¥è¿è¡å®ç°ï¼åæ ·ï¼å¯¹äºèçæ件é´çä¼ è¾ä¹æ¯å个éçï¼éè¿è¾å ¥è¾åºæµæ¥è¿è¡å¤çãä¹åçæä½å°±æ¯è¾å®¹æå¤çäº
èçè天
Android èçå¼ååºæ¬æµç¨
Android èçå¼å(ä¸)-èçç详ç»ä»ç»
åé¢ç两ç¯æç« ï¼ä¸»è¦æ¯å¨ Android å®ç½å ³äºèçä»ç»çåºç¡ä¸å ä¸èªå·±çç解å®æçã主è¦é对çæ¯ Android å¼åä¸çä¸äº API ç使ç¨ã第ä¸ç¯æç« Android èçå¼åï¼ä¸ï¼ 主è¦æ¯ä»ç»äºæ®éçèçå¨ Android å¼åä¸çè¿ç¨ã
第äºç¯æç« Android èçå¼åï¼äºï¼ 主è¦æ¯ä»ç»äºä½åèèççå¼åã
è¿ç¯æç« ä¸»è¦ä»ç»çæ¯èççåå²åä¸äºå ³äºèççéç¨ç¥è¯ï¼è¿æ广æå çç¥è¯ãè¦æ³å½»åºäºè§£èçå¼åï¼è¿äºåºç¡çç¥è¯ä¹æ¯éè¦çï¼å°±åç½ç»åè®®ä¸æ ·ï¼è¿äºé½æ¯åºç¡çå 容ãæ们ç API çè°ç¨é½æ¯ä»¥è¿ä¸ªä¸ºåºç¡çï¼äºè§£è¿äºï¼å¼åè¿ç¨ä¸éå°é®é¢ï¼æå¯ä»¥ç¥éä»ä¹æä¹ä¸åäºã
ä¸ç¯æç« ä¸»è¦è®²çå°±æ¯å®é å¼åä¸çä¸äºåã
èçå ¶å®å°±æ¯ä¸ç§è¿è·ç¦»æ 线éä¿¡ææ¯ã
ä»ä¸å°ä¸åå«ä¸ºï¼æ§å¶å¨ï¼Controllerï¼-->主æºï¼hostï¼-->åºç¨ï¼Applicationï¼
详ç»ä»ç»å个å±çå«ä¹ï¼
BLE åºç¨å¯ä»¥å为两大类ï¼åºäºéè¿æ¥çååºäºè¿æ¥ç
ææå°±æ¯å¤è®¾åå¨è¾¹è®¾å¤ä¸åçè¿æ¥ï¼ä¸»è¦é æ«æå°ç广ææ¥è·åä¿¡æ¯ãåé广æçä¸æ¹å«å broadcaster çå¬å¹¿æçä¸æ¹å«å oberver å¨ GAP å±æ对åºçè§è²å®ä¹ã
ç½ç»ææå¾ï¼
è¿ç§æ¹å¼å°±æ¯å¹¿æ设å¤ä¸æçåå¤åé广æï¼å«æç¹å®çä¿¡æ¯ï¼ï¼ç¶åè§å¯è æ¥åå°å¹¿ææç §ä¸¤è ä¹é´çº¦å®å¥½çåè®®è¿è¡è§£ææ¿å°æç¨çä¿¡æ¯ãä¾å¦ï¼iBeaconï¼éè¿è¿ç§è®¾å¤æ们å¯ä»¥å®ç°å®¤å å®ä½ã
å ¶å®è¿äºè®¾å¤çè§è²å¯ä»¥å³ä½¿å¹¿æè åæ¯è§å¯è ãæ¥æ¶å°å¹¿æåä½åºäºå¤çï¼ç¶åååé广æãè¿æ ·å°±å½¢æäºååçç½ç»ï¼ç±»ä¼¼äºå ç¹ç½ï¼è¿å°±æ¯èç Mesh ç»ç½ã
广ææ°æ®å æ ¼å¼ï¼
æ¯ä¸ªå¹¿ææ°æ®å ç± byte ç»æãå为æææ°æ®åæ ææ°æ®ä¸¤é¨åã
ä¾åï¼
è¿éæ¯æ«æçæ°æ®å ï¼è½¬æ¢æäº è¿å¶ï¼ä¸¤ä¸ªä»£è¡¨ä¸ä¸ªåèï¼ï¼ç¬¬ä¸ä¸ªåèæ¯ è¡¨ç¤ºåé¢ç两个åèæ¯æ°æ®é¨åï¼ç¶å第äºä¸ªåèæ¯ è¡¨ç¤ºäºæ°æ®çç±»åãåé¢ä¸ä¸ªåèå°±æ¯çæ£çæ°æ®äºãè¿ä¸ªå¹¿ææ°æ®åå å°±åæå®äºãä¸é¢å°±æ¯å¦ä¸ä¸ªæ°æ®åå äºãä¾æ¬¡ç±»æ¨ï¼å ³äºæ°æ®ç±»åç解éï¼å®ç½æã
è¿æ¯æ°æ®ç±»å对åºçå«ä¹è¡¨ã
ç½ç»ææå¾ï¼
ä¸ä¸ªä¸å¿è®¾å¤å¯è¿æ¥å¤ä¸ªå¤è®¾ï¼ä½æ¯ä¸ä¸ªå¤è®¾åªè½è¿æ¥ä¸ä¸ªä¸å¿ï¼å¤è®¾è¿æ¥æååå°±ä¼åæ¢å¯¹å¤å¹¿æï¼å«äººå°±åç°ä¸äºå®äºï¼ãå ¶ä¸ä¸ä¸ªä¸å¿è®¾å¤çè¿æ¥å¤è®¾çæ°éä¹æ¯æéçã
é¾æ¥ï¼ /s?__biz=MzU5NzA2NjQzMg==&mid=&idx=1&sn=fd7ebceafeb4cf&scene=#wechat_redirect
2024-12-24 02:11
2024-12-24 02:11
2024-12-24 01:59
2024-12-24 01:41
2024-12-24 01:14
2024-12-24 00:32