国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

手機開發(fā) - Android藍牙模塊連接后怎么接收數(shù)據(jù)?求助

瀏覽:157日期:2024-08-26 15:33:16

問題描述

package myapplication.com.myapplicationble;import android.app.Service;import android.bluetooth.BluetoothAdapter;import android.bluetooth.BluetoothDevice;import android.bluetooth.BluetoothServerSocket;import android.bluetooth.BluetoothSocket;import android.content.Context;import android.content.Intent;import android.os.AsyncTask;import android.os.Handler;import android.os.IBinder;import android.os.Message;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.PopupMenu;import android.widget.PopupWindow;import android.widget.TextView;import android.widget.Toast;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.Socket;import java.util.UUID;public class MainActivity extends AppCompatActivity { private BluetoothAdapter BA; BluetoothServerSocket mmServerSocket; private static String address = '20:16:09:26:81:80'; // <==應(yīng)填寫藍牙串口模塊的藍牙地址。 private BluetoothSocket btSocket = null; private OutputStream outStream = null; public InputStream inStream = null; EditText editText; TextView textView; Button button, button1; private static final UUID MY_UUID = UUID.fromString('00001101-0000-1000-8000-00805F9B34FB'); @Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView(); } /** * 連接藍牙 * **/ public void connect() {BA = BluetoothAdapter.getDefaultAdapter();BluetoothDevice device = BA.getRemoteDevice(address);// pairedDevices = BA.getBondedDevices();try { btSocket = device.createRfcommSocketToServiceRecord(MY_UUID); new ReceiveDatas(btSocket).start();} catch (IOException e) {}BA.cancelDiscovery();try { btSocket.connect(); String s='smn'; outStream = btSocket.getOutputStream(); outStream.write(s.getBytes()); //outStream.write(0x41); inStream = btSocket.getInputStream(); Jieshou(); // System.out.println('**' + inStream.toString()); // System.out.println('**' + inStream.read()); // int x = inStream.read(); //outStream.write(x+1); // System.out.println('****--'+x);} catch (IOException e) {}try { // btSocket.close();} catch (Exception e2) { // Log .e(TAG,'ON RESUME: Unable to close socket during connection failure', e2);} } public void initView() {editText = (EditText) findViewById(R.id.edit);textView = (TextView) findViewById(R.id.textView);button = (Button) findViewById(R.id.button);button1 = (Button) findViewById(R.id.button1);BA = BluetoothAdapter.getDefaultAdapter(); /*** 連接藍牙* */button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {System.out.println('***11');as(); }});/** * 發(fā)送信息 可以發(fā)送了 * **/button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {String s = editText.getText().toString().trim();try { outStream = btSocket.getOutputStream(); inStream=btSocket.getInputStream(); outStream.write(s.getBytes());} catch (IOException e) { e.printStackTrace();} }}); }/** * * 連接藍牙 * **/ public void as() {new AsyncTask() { @Override protected String doInBackground(Object[] params) {connect();return null; } @Override protected void onPreExecute() {super.onPreExecute(); } @Override protected void onPostExecute(Object o) {super.onPostExecute(o);Toast.makeText(getApplicationContext(), '2', Toast.LENGTH_SHORT).show(); }}.execute(); }/** * * */ public void Jieshou() {new Thread(new Runnable() { @Override public void run() {while(true){ ByteArrayOutputStream byteArrayOutputStream=null; byteArrayOutputStream = new ByteArrayOutputStream(); try {inStream=btSocket.getInputStream(); } catch (IOException e) {e.printStackTrace(); } byte[] buffer = new byte[1024];// 緩沖數(shù)據(jù)流 int temp = 0; // while( (temp = inStream.read(buffer)) != -1) { // os.write(buffer, 0, temp); byteArrayOutputStream.write(buffer, 0, temp); // } System.out.println('*shuju*'+byteArrayOutputStream.toByteArray().toString()); try {Thread.sleep(1000); } catch (InterruptedException e) {e.printStackTrace(); }} }}).start(); } }

已經(jīng)可以發(fā)送信息到藍牙了,可是怎么接收數(shù)據(jù)???求助

問題解答

回答1:

自己找了個接受數(shù)據(jù)的線程……

private class ConnectedThread extends Thread { private final BluetoothSocket socket; private final InputStream inputStream; private final OutputStream outputStream; public ConnectedThread(BluetoothSocket socket) { this.socket = socket; InputStream input = null; OutputStream output = null; try { input = socket.getInputStream(); output = socket.getOutputStream(); } catch (IOException e) { e.printStackTrace(); } this.inputStream = input; this.outputStream = output; } public void run() { byte[] buff = new byte[1024]; int bytes; while (true) { try { bytes = inputStream.read(buff); String str = new String(buff, 'ISO-8859-1'); str = str.substring(0, bytes); Log.e('recv', str); Message message=handler.obtainMessage(); message.obj=str; handler.sendMessage(message); } catch (IOException e) { e.printStackTrace(); break; } } } public void write(byte[] bytes) { try { outputStream.write(bytes); } catch (IOException e) { e.printStackTrace(); } } public void cancel() { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } }回答2:

感覺你的寫法有點奇怪……

手機開發(fā) - Android藍牙模塊連接后怎么接收數(shù)據(jù)?求助

mBluetoothGattCallback 是 BluetoothGattCallback的實現(xiàn),用于回調(diào)連接狀態(tài)和傳輸數(shù)據(jù),具體你可以百度一下或看官方文檔

相關(guān)文章:
主站蜘蛛池模板: 亚洲成年人专区 | 暴操美女| 亚洲成a v人片在线观看 | 一区二区在线看 | 高清欧美性xxxx成熟 | a毛片在线还看免费网站 | 欧美一级xxx | 日本午色www高清视频 | 91在线免费公开视频 | 91免费观看视频 | 日本免费人成黄页网观看视频 | 国产视频久 | 中文字幕在线看片成人 | 亚洲加勒比久久88色综合1 | 欧美成年视频 | 日本亚洲欧美高清专区vr专区 | 美女视频网站黄色 | 欧美日产国产亚洲综合图区一 | 国产精品视_精品国产免费 国产精品视频久 | 成人精品在线观看 | 香港三澳门三日本三级 | 国产色手机在线观看播放 | 免费毛片网站 | 免费观看欧美一区二区三区 | 国产成人精品男人的天堂网站 | 久久精品a一国产成人免费网站 | 成人一级视频 | 美女张开腿黄网站免费国产 | 久久综合九色综合欧洲色 | 欧美特级一级毛片 | 日韩国产欧美一区二区三区 | 亚洲视频日韩视频 | 中文字幕二区 | www.碰| 日本特黄特色免费大片 | 寂寞午夜影院 | 久久久视 | 欧美精品做人一级爱免费 | 国产原创在线视频 | 日韩特级毛片免费观看视频 | 99视频久久精品久久 |