BluetoothPan\u7684connect\u63a5\u53e3\u662f\u7528\u6765\u8fde\u63a5\u5230\u8fdc\u7a0b\u8bbe\u5907\u7684\u7c73\u7269\u4e2a\u4eba\u533a\u57df\u7f51\u7edc\uff08PAN\uff09\u7684\u3002\n\n\u5728Android\u4e2d\uff0c\u901a\u8fc7BluetoothAdapter\u7684getDefaultAdapter\uff08\uff09\u65b9\u6cd5\u83b7\u5f97\u5230BluetoothAdapter\u5bf9\u8c61\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528BluetoothAdapter\u7684getProfileProxy\uff08\uff09\u65b9\u6cd5\u83b7\u5f97BluetoothProfile\u5bf9\u8c61\uff0c\u5176\u4e2d\u53c2\u6570profile\u662fBluetoothProfile\u3002PAN\u7684\u5e38\u91cf\u503c\uff0c\u8868\u793a\u83b7\u5f97\u7c73\u7269PAN\u7684\u4ee3\u7406\u3002\n\n\u83b7\u5f97\u5230BluetoothPan\u5bf9\u8c61\u540e\uff0c\u5c31\u53ef\u4ee5\u8c03\u7528\u5176connect\uff08\uff09\u65b9\u6cd5\u8fdb\u884c\u8fde\u63a5\u64cd\u4f5c\u3002connect\uff08\uff09\u65b9\u6cd5\u7684\u53c2\u6570\u4e3aBluetoothDevice\u5bf9\u8c61\uff0c\u8868\u793a\u8981\u8fde\u63a5\u7684\u8fdc\u7a0b\u8bbe\u5907\u3002\u5728\u8fde\u63a5\u4e4b\u524d\uff0c\u9700\u8981\u5148\u8c03\u7528BluetoothDevice\u7684createBond\uff08\uff09\u65b9\u6cd5\u8fdb\u884c\u914d\u5bf9\u64cd\u4f5c\uff0c\u786e\u4fdd\u8bbe\u5907\u4e4b\u95f4\u7684\u4fe1\u4efb\u5173\u7cfb\u3002\n\n\u8fde\u63a5\u6210\u529f\u540e\uff0c\u4f1a\u89e6\u52a8BluetoothProfile\u3002ServiceListener\u7684onServiceConnected\uff08\uff09\u56de\u8c03\u65b9\u6cd5\u3002\u5728\u8be5\u56de\u8c03\u65b9\u6cd5\u4e2d\uff0c\u53ef\u4ee5\u83b7\u5f97\u5230BluetoothPan\u5bf9\u8c61\uff0c\u5e76\u8fdb\u884c\u540e\u7eed\u64cd\u4f5c\uff0c\u4f8b\u5982\u8bbe\u7f6e\u8fde\u63a5\u7684\u7f51\u7edc\u914d\u7f6e\u7b49\u3002\n\n\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a\n\njava\nBluetoothAdapter bluetoothAdapter = BluetoothAdapter\u3002getDefaultAdapter\uff08\uff09\uff1b\nbluetoothAdapter\u3002getProfileProxy\uff08context\uff0c new BluetoothProfile\u3002ServiceListener\uff08\uff09 {\n\t\u0040Override\n\tpublic void onServiceConnected\uff08int profile\uff0c BluetoothProfile proxy\uff09 {\n\t if (profile == BluetoothProfile\u3002PAN) {\n\t BluetoothPan bluetoothPan = \uff08BluetoothPan\uff09 proxy\uff1b\n\t BluetoothDevice device = bluetoothAdapter\u3002getRemoteDevice\uff08deviceAddress\uff09\uff1b\n\t device\u3002createBond\uff08\uff09\uff1b\n\n\t bluetoothPan\u3002connect\uff08device\uff09\uff1b\n\t }\n\t}\n\n\t\u0040Override\n\tpublic void onServiceDisconnected\uff08int profile\uff09 {\n\t // \u65ad\u5f00\u8fde\u63a5\u65f6\u7684\u5904\u7406\n\t}\n}\uff0c BluetoothProfile\u3002PAN\uff09\uff1b\n\n\n\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fde\u63a5\u64cd\u4f5c\u53ef\u80fd\u4f1a\u6d89\u53ca\u5230\u4e00\u4e9b\u6743\u9650\u7684\u7533\u8bf7\uff0c\u4f8b\u5982BLUETOOTH\u548cBLUETOOTH_ADMIN\u6743\u9650\uff0c\u4ee5\u53caACCESS_FINE_LOCATION\u6216ACCESS_COARSE_LOCATION\u6743\u9650\uff08\u9700\u8981\u5728AndroidManifest\u3002xml\u4e2d\u58f0\u660e\u5e76\u52a8\u6001\u7533\u8bf7\uff09\u3002\n\n\u8d85\u52a0\u4e4b\u5916\uff0c\u8fd8\u9700\u8981\u5728AndroidManifest\u3002xml\u6587\u4ef6\u4e2d\u58f0\u660e\u76f8\u5173\u7684\u6743\u9650\u548c\u670d\u52a1\uff1a\n\nxml\n<uses-permission android:name="android.permission.BLUETOOTH" />\n<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />\n<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />\n\n<service android:name=".BluetoothPanService"\n android:enabled="true"\n android:exported="true">\n <intent-filter>\n <action android:name="android.bluetooth.pan.profile.action.CONNECTION_STATE_CHANGED" />\n </intent-filter>\n</service>\n\n\n\u4ee5\u4e0a\u5c31\u662f\u5173\u4e8eBluetoothPan\u7684connect\u63a5\u53e3\u7684\u8be6\u7ec6\u4ecb\u7ecd\u3002\u901a\u8fc7\u8be5\u63a5\u53e3\uff0c\u53ef\u4ee5\u5b9e\u73b0Android\u8bbe\u5907\u4e0e\u8fdc\u7a0b\u8bbe\u5907\u4e4b\u95f4\u7684\u7c73\u7269PAN\u8fde\u63a5\u3002


原文地址: https://www.cveoy.top/t/topic/pKFD 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录