android蓝牙串口广播连接,Android蓝牙连接与串口设备

我已经使用蓝牙连接的BluetoothChat示例程序的BluetoothChatService类。我已经像Android蓝牙连接与串口设备改性它

私有静态最后UUID MY_UUID = UUID.fromString( “00001101-0000-1000-8000-00805F9B34FB”);

将其连接到串行端口设备。

我的样品测试Android设备是NexusOne,HTC Desire,LG Optimus,摩托罗拉Droid。

每当我使用我的应用程序将其连接到硬件时,它都会使用NexusOne正确连接和断开连接。但是,当他们连接时使用其他Android设备时,有时甚至在尝试100次后都无法连接。有时当我断开连接时,应用程序断开连接,但硬件上的蓝牙指示灯亮起,表示连接仍处于开启状态。我想知道如果我的编码错误,或硬件错误,或Android操作系统蓝牙库错误。我没有面对NexusOne的这个问题。我一直无法确定问题出在哪里的确切位置。

有人可以指出我应该采取什么行动来解决这个问题吗?给出

代码 “无法连接装置” 吐司消息

/**

* Constructor. Prepares a new BluetoothChat session.

* @param context The UI Activity Context

* @param handler A Handler to send messages back to the UI Activity

*/

public BluetoothChatService(Context context, Handler handler) {

mAdapter = BluetoothAdapter.getDefaultAdapter();

mState = STATE_NONE;

mHandler = handler;

}

/**

* Set the current state of the chat connection

* @param state An integer defining the current connection state

*/

private synchronized void setState(int state) {

if (D) Log.d(TAG, "setState() " + mState + " -> " + state);

mState = state;

// Give the new state to the Handler so the UI Activity can update

mHandler.obtainMessage(BluetoothChat.MESSAGE_STATE_CHANGE, state, -1).sendToTarget();

}

/**

* Return the current connection state. */

public synchronized int getState() {

return mState;

}

/**

* 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();

}

}

/**

* Start the ConnectThread to initiate a connection to a remote device.

* @param device The BluetoothDevice to connect

* @param secure Socket Security type - Secure (true) , Insecure (false)

*/

public synchronized void connect(BluetoothDevice device, boolean secure) {

if (D) Log.d(TAG, "connect to: " + device);

// Cancel any thread attempting to make a connection

if (mState == STATE_CONNECTING) {

if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;}

}

// Cancel any thread currently running a connection

if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}

// Start the thread to connect with the given device

mConnectThread = new ConnectThread(device, secure);

mConnectThread.start();

setState(STATE_CONNECTING);

}

/**

* Start the ConnectedThread to begin managing a Bluetooth connection

* @param socket The BluetoothSocket on which the connection was made

* @param device The BluetoothDevice that has been connected

*/

public synchronized void connected(BluetoothSocket socket, BluetoothDevice

device, final String socketType) {

if (D) Log.d(TAG, "connected, Socket Type:" + socketType);

// Cancel the thread that completed the connection

if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;}

// Cancel any thread currently running a connection

if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}

// Cancel the accept thread because we only want to connect to one device

if (mSecureAcceptThread != null) {

mSecureAcceptThread.cancel();

mSecureAcceptThread = null;

}

if (mInsecureAcceptThread != null) {

mInsecureAcceptThread.cancel();

mInsecureAcceptThread = null;

}

// Start the thread to manage the connection and perform transmissions

mConnectedThread = new ConnectedThread(socket, socketType);

mConnectedThread.start();

// Send the name of the connected device back to the UI Activity

Message msg = mHandler.obtainMessage(BluetoothChat.MESSAGE_DEVICE_NAME);

Bundle bundle = new Bundle();

bundle.putString(BluetoothChat.DEVICE_NAME, device.getName());

msg.setData(bundle);

mHandler.sendMessage(msg);

setState(STATE_CONNECTED);

}

/**

* Stop all threads

*/

public synchronized void stop() {

if (D) Log.d(TAG, "stop");

if (mConnectThread != null) {

mConnectThread.cancel();

mConnectThread = null;

}

if (mConnectedThread != null) {

mConnectedThread.cancel();

mConnectedThread = null;

}

if (mSecureAcceptThread != null) {

mSecureAcceptThread.cancel();

mSecureAcceptThread = null;

}

if (mInsecureAcceptThread != null) {

mInsecureAcceptThread.cancel();

mInsecureAcceptThread = null;

}

setState(STATE_NONE);

}

/**

* Write to the ConnectedThread in an unsynchronized manner

* @param out The bytes to write

* @see ConnectedThread#write(byte[])

*/

public void write(byte[] out) {

// Create temporary object

ConnectedThread r;

// Synchronize a copy of the ConnectedThread

synchronized (this) {

if (mState != STATE_CONNECTED) return;

r = mConnectedThread;

}

// Perform the write unsynchronized

r.write(out);

}

/**

* Indicate that the connection attempt failed and notify the UI Activity.

*/

private void connectionFailed() {

// Send a failure message back to the Activity

Message msg = mHandler.obtainMessage(BluetoothChat.MESSAGE_TOAST);

Bundle bundle = new Bundle();

bundle.putString(BluetoothChat.TOAST, "Unable to connect device");

msg.setData(bundle);

mHandler.sendMessage(msg);

// Start the service over to restart listening mode

BluetoothChatService.this.start();

}

/**

* Indicate that the connection was lost and notify the UI Activity.

*/

private void connectionLost() {

// Send a failure message back to the Activity

Message msg = mHandler.obtainMessage(BluetoothChat.MESSAGE_TOAST);

Bundle bundle = new Bundle();

bundle.putString(BluetoothChat.TOAST, "Device connection was lost");

msg.setData(bundle);

mHandler.sendMessage(msg);

// Start the service over to restart listening mode

BluetoothChatService.this.start();

}

+1

什么是硬件?你有Android蓝牙日志,你可以发布显示问题。你的代码是做什么的?请张贴您希望我们查看的代码部分 –

2011-03-18 15:55:48

+0

它是一个定制的硬件,不管日志是否显示任何错误跟踪。当我尝试除NexusOne之外连接它们时,我会收到Toast消息“无法连接设备”。我在我的问题中添加了代码段,这导致了这个Toast消息。 –

2011-03-19 08:34:24

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值