android 怎么通过蓝牙向一个硬件发送AT指令

 我来答
lihongwei41
2016-04-26 · TA获得超过4万个赞
知道大有可为答主
回答量:2.5万
采纳率:0%
帮助的人:6044万
展开全部

通过BluetoothSocket的OutputStream发送过去后,InputStream获取不到数据...代码用的android自带的蓝牙例子.这样发送指令后

?12String string = "41542B50494F392C310D";mmOutStream.write(string.getBytes());读取inputsteam中的?1Log.d("example", "do read");

不执行,完整代码如下:

?1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980/** * This thread runs during a connection with a remote device. It handles all * incoming and outgoing transmissions. */private class ConnectedThread extends Thread {    private final BluetoothSocket mmSocket;    private final InputStream mmInStream;    private final OutputStream mmOutStream;     public ConnectedThread(BluetoothSocket socket, String socketType) {        Log.d(TAG, "create ConnectedThread: " + socketType);        mmSocket = socket;        InputStream tmpIn = null;        OutputStream tmpOut = null;         // Get the BluetoothSocket input and output streams        try {            tmpIn = socket.getInputStream();            tmpOut = socket.getOutputStream();        } catch (IOException e) {            Log.e(TAG, "temp sockets not created", e);        }         mmInStream = tmpIn;        mmOutStream = tmpOut;    }     public void run() {        Log.i(TAG, "BEGIN mConnectedThread");        byte[] buffer = new byte[1024];        int bytes;         // Keep listening to the InputStream while connected        while (true) {            Log.d("example", "do read");            try {                // Read from the InputStream                bytes = mmInStream.read(buffer);                 // Send the obtained bytes to the UI Activity                mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes,                        -1, buffer).sendToTarget();            } catch (IOException e) {                Log.e(TAG, "disconnected", e);                connectionLost();                // Start the service over to restart listening mode                BluetoothChatService.this.start();                break;            }        }    }     /**     * Write to the connected OutStream.     *      * @param buffer     *            The bytes to write     */    public void write(byte[] buffer) {         String string = "41542B50494F392C310D";        try {            mmOutStream.write(string.getBytes());             // Share the sent message back to the UI Activity            mHandler.obtainMessage(BluetoothChat.MESSAGE_WRITE, -1, -1,                    buffer).sendToTarget();        } catch (IOException e) {            Log.e(TAG, "Exception during write", e);        }    }     public void cancel() {        try {            mmSocket.close();        } catch (IOException e) {            Log.e(TAG, "close() of connect socket failed", e);        }    }}




0收藏(0)
  •  
育知同创教育
2016-04-26 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
展开全部
将16进制的字符串转换成bytes,通过hexstring2bytes转换而不能直接用getbytes
String string = "41542B50494F392C310D";
mmOutStream.write(string.getBytes());

读取inputsteam中的
?
Log.d("example", "do read");

不执行,完整代码如下:

?

/**
* This thread runs during a connection with a remote device. It handles all
* incoming and outgoing transmissions.
*/
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;

public ConnectedThread(BluetoothSocket socket, String socketType) {
Log.d(TAG, "create ConnectedThread: " + socketType);
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;

// Get the BluetoothSocket input and output streams
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "temp sockets not created", e);
}

mmInStream = tmpIn;
mmOutStream = tmpOut;
}

public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[1024];
int bytes;

// Keep listening to the InputStream while connected
while (true) {
Log.d("example", "do read");
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);

// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes,
-1, buffer).sendToTarget();
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
// Start the service over to restart listening mode
BluetoothChatService.this.start();
break;
}
}
}

/**
* Write to the connected OutStream.
*
* @param buffer
* The bytes to write
*/
public void write(byte[] buffer) {

String string = "41542B50494F392C310D";
try {
mmOutStream.write(string.getBytes());

// Share the sent message back to the UI Activity
mHandler.obtainMessage(BluetoothChat.MESSAGE_WRITE, -1, -1,
buffer).sendToTarget();
} catch (IOException e) {
Log.e(TAG, "Exception during write", e);
}
}

public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
Log.e(TAG, "close() of connect socket failed", e);
}
}
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式