2015-05-14 38 views
1

我可以在沒有任何提示的情況下使用以下代碼打開/關閉藍牙。它需要BLUETOOTHBLUETOOTH_ADMIN權限。Android中沒有提示的藍牙發現

boolean isEnabled = bluetoothAdapter.isEnabled(); 
if (enable && !isEnabled) { 
    return bluetoothAdapter.enable(); 
} else if (!enable && isEnabled) { 
    return bluetoothAdapter.disable(); 
} 

但沒有找到任何方式來設置藍牙可發現沒有用戶提示。它有線提醒用戶每次。沒有「不要再問我」功能,我害怕。有沒有什麼好方法可以讓藍牙設備被發現?我不關心持續時間。另外我的設備沒有紮根。

更多信息

我發現BluetoothAdapter.java源代碼,它有一個名爲setDiscoverableDuration的公共方法。但爲什麼我無法訪問它?爲什麼一些公開的方法隱藏在Api文檔中?他們怎麼做到的?所有方法都是公開的。

+0

不知道如果我理解正確的話,但發現適用於遠程設備,它必須是能夠與掃描找到它發現。 – JPS

+0

@JPS使用可發現的我的意思是我們的設備應該在其他設備掃描時找到。 – shantanu

+1

簽出這個,似乎是隱藏的:http://stackoverflow.com/questions/3190623/make-bluetooth-on-android-2-1-discoverable-indefinitely – JPS

回答

8

最後,我已經找到一種方法來做到這一點使用反射。

Method method; 
try { 
    method = bluetoothAdapter.getClass().getMethod("setScanMode", int.class, int.class); 
    method.invoke(bluetoothAdapter,BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE,120); 
    Log.e("invoke","method invoke successfully"); 
} 
catch (Exception e){ 
    e.printStackTrace(); 
} 

警告:上述方法試圖調用隱藏方法。所以在將來也許它不會起作用。

+1

這個作品,應該被接受爲答案 編輯:哎呀,你是OP – behelit