Unity 藍牙插件

一、新建一個Unity5.6.2f1工程,導入正版Bluetooth LE for iOS tvOS and Android.unitypackage
二、用JD-GUI反編譯工具查看unityandroidbluetoothlelib.jar
三、將反編譯的代碼拷貝出來,創建對應的文件目錄,
四、在Eclipse中創建空工程,將代碼文件放進去。
五、修改AndroidManifest.xml,添加藍牙權限
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-feature android:name="android.hardware.bluetooth_le" android:required="false"/>
六、將unity-classes.jar文件添加到libs目錄下.這個jar文件能夠從Unity導出一個Android工程中找到
七、修改其中的3處錯誤
(A)註釋的這個
//import android.bluetooth.le.ScanSettings.Builder;
(B)
private UUID getFullBluetoothLEUUID(String uuidString)函數中UUID uuid;重定義了
(C)
public class AdRecordList類中的
this.Records.add(new UnityBluetoothLE.AdRecord(UnityBluetoothLE.this, length, type, data));
改成this.Records.add(new UnityBluetoothLE.AdRecord(length, type, data));android

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++重點來了,LEKE山寨機Q9自帶的didoOS6.5系統不能執行藍牙掃描功能函數+++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
我要作的就是修好這個BUG!!!api

之因此能夠肯定能夠修好這個BUG,是由於有一款經過藍牙控制機器人的APP裝在這台山寨機上能夠進行藍牙通訊函數

通過幾天的縝密調試測試,發現那款APP也只能鏈接已經配對好的機器人藍牙!!!
找到突破口!既然這貨不能經過程序API掃描周圍藍牙設備,那就用它自帶的系統藍牙掃描(自帶的系統藍牙掃描仍是能用的)
在程序中獲取已經配對的藍牙設備。
GOOOOOOOD!!
網上找到相關代碼碎片,拿過來測試,經過!!
在private void api21Scan(List<UUID> uuids)開頭添加如下代碼:
//獲得全部已配對的藍牙適配器對象
Set<BluetoothDevice> devices = mBluetoothAdapter.getBondedDevices();
if(devices.size()>0){
for(Iterator iterator = devices.iterator();iterator.hasNext();){
BluetoothDevice bluetoothDevice = (BluetoothDevice) iterator.next();
//Toast.makeText(mContext, "AA>>>"+bluetoothDevice.getAddress(), Toast.LENGTH_SHORT).show();
// 給Unity發送過去
UnityBluetoothLE.this.sendDiscoveredDevice(bluetoothDevice, 0, null);
}
}工具


Eclipse中打包jar步驟:
項目名右鍵-選擇Properties-勾選Is Library-Apply-OK
項目名右鍵-Build Project,將會在bin目錄下生成一個.jar文件測試

最後將工程打包成一個jar文件,替換掉Unity工程中的unityandroidbluetoothlelib.jarui

相關文章
相關標籤/搜索