我有一個非常特別的Android的藍牙問題:具體Android的藍牙服務器問題
我有一個設備(幾乎沒有文檔)充當藍牙客戶端,並嘗試連接(配對後),以藍牙Server中使用UUID「1234」進行偵聽。我已經預先使用python腳本進行了測試,該設備工作並且連接到服務器。從腳本中的相關代碼:
import bluetooth
uuid = "1234"
pc = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
pc.bind(("", bluetooth.PORT_ANY))
pc.listen(1)
port = pc.getsockname()[1]
print 'Active RFCOMM port: ', port, '\r\n'
bluetooth.advertise_service(pc, "Server",
service_id = uuid,
service_classes = [ uuid, bluetooth.SERIAL_PORT_CLASS ],
profiles = [ bluetooth.SERIAL_PORT_PROFILE ]
)
print 'Waiting for connection...\r\n'
address = pc.accept()
print 'Accepting connection from: ', address, '\r\n'
我想創建使用BluetoothServerSocket在Android類似的服務器。我已將根據此article的16位UUID(「1234」)轉換爲「000-0000-1000-8000-00805F9B34FB」。我用python腳本測試過服務器是有效的,並且可以使用指定的UUID連接到它。使用的代碼是非常標準的,因爲它可以在互聯網的許多例子中找到:
@Override
public void run() {
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
Timber.i("Start Server");
try {
final BluetoothServerSocket bluetoothServer = adapter.listenUsingRfcommWithServiceRecord("Server", mUUID);
Timber.i("Server Started: " + mUUID.toString());
BluetoothSocket socket = bluetoothServer.accept();
Timber.i("Socket initiated: " + socket.getRemoteDevice().getName());
bluetoothServer.close();
} catch (IOException e) {
Timber.e("Error starting server: " + e.getMessage());
}
}
我試圖與這兩個listenUsingRfcommWithServiceRecord
和listenUsingInsecureRfcommWithServiceRecord
。我確定手機和設備已配對,並且手機可以發現。我曾嘗試過使用不同操作系統版本(2.3.7,4.0.3,4.4.2,5.1.0)的不同手機,因爲我認爲Android 4.2中藍牙堆棧的變化是個問題。似乎沒有任何東西讓設備連接到我的手機。我還可以在設備嘗試連接時從我的Nexus 5中提供一個hci轉儲,但我無法確定問題。
任何幫助,將不勝感激。
編輯:
這是用於驗證Android的藍牙服務器的Python代碼:
import sys
import bluetooth
uuid = "1234"
service_matches = bluetooth.find_service(uuid = uuid)
if len(service_matches) == 0:
print "couldn't find the service"
sys.exit(0)
first_match = service_matches[0]
port = first_match["port"]
name = first_match["name"]
host = first_match["host"]
print "connecting to \"%s\" on %s" % (name, host)
sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sock.connect((host, port))
sock.send("Test")
sock.close()
不用說,它的工作沒有任何問題。
請張貼您的客戶代碼嗎? – 7383