有誰知道任何可用的示例,說明Android上的藍牙開發。適用於Android的藍牙示例
我已閱讀教程here,我瞭解該頁面上的所有內容。
但是,當涉及到實施藍牙代碼時,需要查看藍牙聊天示例以瞭解它是如何工作的。
藍牙聊天例如here
這個例子是好的,但也很難理解,因爲每個設備最初設置爲服務器。
誰是服務器,並且兩臺設備都會發送服務器套接字,直到一臺設備掃描完成爲止?
一旦設備自己發現它會成爲服務器?
什麼時候OnResume活動開始,因爲一旦啓動並且mChatService已經在SetupChat中初始化,設備將啓動一個Accept線程。
下面給出了一些代碼示例,上面提供了完整的藍牙聊天鏈接。
@Override
public synchronized void onResume() {
super.onResume();
if(D) Log.e(TAG, "+ ON RESUME +");
// Performing this check in onResume() covers the case in which BT was
// not enabled during onStart(), so we were paused to enable it...
// onResume() will be called when ACTION_REQUEST_ENABLE activity returns.
if (mChatService != null) {
// Only if the state is STATE_NONE, do we know that we haven't started already
if (mChatService.getState() == BluetoothChatService.STATE_NONE) {
// Start the Bluetooth chat services
mChatService.start();
}
}
}
private void setupChat() {
// Initialize the BluetoothChatService to perform bluetooth connections
mChatService = new BluetoothChatService(this, mHandler);
// Initialize the buffer for outgoing messages
mOutStringBuffer = new StringBuffer("");
}
/**
* Start the chat service. Specifically start AcceptThread to begin a
* session in listening (server) mode. Called by the Activity onResume() */
public synchronized void start() {
if (D) Log.d(TAG, "start");
// Cancel any thread attempting to make a connection
if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;}
// Cancel any thread currently running a connection
if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}
setState(STATE_LISTEN);
// Start the thread to listen on a BluetoothServerSocket
if (mSecureAcceptThread == null) {
mSecureAcceptThread = new AcceptThread(true);
mSecureAcceptThread.start();
}
if (mInsecureAcceptThread == null) {
mInsecureAcceptThread = new AcceptThread(false);
mInsecureAcceptThread.start();
}
}
我所要求的是明確分離服務器端和藍牙的客戶端藍牙的任何例子,更易於理解和實例。 我已經Google'd這個,並且我已經閱讀了developer.android.com網站上的所有詳細信息。
注意:如果使用的HTC Hero有一個與Android的藍牙問題。上面的代碼無法正確工作。 – Navigatron 2013-06-04 13:35:11