亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

android實現手機與單片機藍牙模塊通信

發布時間:2020-09-19 17:22:13 來源:腳本之家 閱讀:150 作者:馮爽朗 欄目:移動開發

本文實例為大家分享了android實現手機與單片機藍牙模塊通信的具體代碼,供大家參考,具體內容如下

我是參考原博客的內容去寫的,由于原博客寫的不全,少了關鍵的幾個類,然后我就憑借自己扎實的功底補出來了,現在藍牙工作正常,能發能收!在看這邊文章之前你要先了解一下藍牙的工作狀態,我的代碼里面可能解釋的不是很詳細,但是我自己是能看懂的!

package com.example.fsl.bluetooth; 
 
import android.app.Activity; 
import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.bluetooth.BluetoothSocket; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.os.Handler; 
import android.os.Message; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.support.v7.widget.Toolbar; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ListView; 
import android.widget.ProgressBar; 
import android.widget.TextView; 
import android.widget.Toast; 
 
import java.util.ArrayList; 
import java.util.List; 
import java.util.UUID; 
 
public class MainActivity extends AppCompatActivity { 
 private Toolbar toolbar; 
 private TextView status; 
 private StringBuilder mstringbuilder; 
 private static final UUID MY_UUID=UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");//沒有用到 
 BluetoothReceiver receiver; 
 BluetoothAdapter mBtAdapter; 
 BluetoothSocket mBtSocket; 
 private BlueToothTool client; 
 private ListView mListView; 
 private List<String> ListDevice; 
 private ArrayAdapter<String> mAdapter; 
 private Button mbutton; 
 private EditText editText; 
 private ProgressBar progressBar; 
 private LoopProgressBar loopProgressBar; 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
 super.onCreate(savedInstanceState); 
 setContentView(R.layout.activity_main); 
 toolbar=(Toolbar)findViewById(R.id.toolbar); 
 status=(TextView)findViewById(R.id.textView2); 
 mListView=(ListView)findViewById(R.id.listView); 
 mbutton=(Button)findViewById(R.id.button); 
 editText=(EditText)findViewById(R.id.editText); 
 progressBar=(ProgressBar)findViewById(R.id.progressBar); 
 progressBar.setVisibility(View.INVISIBLE); 
 loopProgressBar=(LoopProgressBar)findViewById(R.id.loop); 
 ListDevice=new ArrayList<String>(); 
 mstringbuilder=new StringBuilder(); 
 setSupportActionBar(toolbar); 
 enablebluetooth(); 
 mbutton.setOnClickListener(new View.OnClickListener() { 
 @Override 
 public void onClick(View v) { 
 BlueToothTool.WriteTask W=client.new WriteTask(editText.getText().toString()); 
 W.start(); 
 } 
 }); 
 mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
 @Override 
 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
 mBtAdapter.cancelDiscovery();//停止搜索 
 progressBar.setVisibility(View.INVISIBLE); 
 String str = ListDevice.get(position); 
 String macAdress = str.split("\\|")[1]; 
 BluetoothDevice device = mBtAdapter.getRemoteDevice(macAdress); 
 client=new BlueToothTool(device,handler); 
 try{ 
  client.connect(); 
 }catch (Exception e){ 
  e.printStackTrace(); 
 } 
 } 
 }); 
 } 
 
 /** 
 *開啟藍牙且被發現 
 */ 
 private void enablebluetooth(){ 
 mBtAdapter=BluetoothAdapter.getDefaultAdapter(); 
 
 /** 
 *if(!mBtAdapter.isEnabled()){這里可以先使能,可以在REQUEST_DISCOVERABLE處使能,這樣的話可以連使能和請求被發現一塊完成 
 // mBtAdapter.enable(); 
 Intent enableIntent=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
 startActivityForResult(enableIntent,REQUEST_ENABLE); 
 } 
 else { 
 show("藍牙已開啟"); 
 }*/ 
 Intent enable = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); 
 enable.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); 
 startActivityForResult(enable, REQUEST_DISCOVERABLE); 
 } 
 
 /** 
 * 銷毀事件,注銷廣播 
 */ 
 @Override 
 protected void onDestroy() { 
 unregisterReceiver(receiver); 
 super.onDestroy(); 
 } 
 private final Handler handler = new Handler() { 
 @Override 
 public void handleMessage(Message msg) { 
 switch (msg.what) { 
 case BlueToothTool.CONNECT_FAILED: 
  show("連接失敗"); 
  try { 
  client.connect(); 
  } catch (Exception e) { 
  Log.e("TAG", e.toString()); 
  } 
  break; 
 case BlueToothTool.CONNECT_SUCCESS: 
  show("連接成功"); 
  mListView.setVisibility(View.INVISIBLE); 
  break; 
 case BlueToothTool.READ_FAILED: 
  show("讀取失敗"); 
  break; 
 case BlueToothTool.WRITE_FAILED: 
  show("寫入失敗"); 
  break; 
 case BlueToothTool.DATA: 
  mstringbuilder.append(msg.obj.toString()); 
  show(mstringbuilder.toString()); 
  break; 
 } 
 } 
 }; 
 /** 
 * 請求響應結果 
 * @param requestCode 
 * @param resultCode 
 * @param data 
 */ 
 @Override 
 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
 switch (requestCode){ 
 
 /** 
 *case REQUEST_ENABLE: 
 if(requestCode!= Activity.RESULT_OK){ 
  show("藍牙未開啟"); 
 } 
 else 
  show("藍牙已開啟"); 
 break;*/ 
 case REQUEST_DISCOVERABLE: 
 if(resultCode==Activity.RESULT_CANCELED){ 
  show("藍牙未開啟"); 
 } 
 else 
  show("藍牙已開啟"); 
 break; 
 default: 
  break; 
 } 
 } 
 public boolean onCreateOptionsMenu(Menu menu){ 
 getMenuInflater().inflate(R.menu.menu,menu); 
 return true; 
 } 
 private static final int REQUEST_ENABLE=1; 
 private static final int REQUEST_DISCOVERABLE=2; 
 
 /** 
 * 注冊廣播事件 
 */ 
 @Override 
 public void onResume(){ 
 super.onResume(); 
 IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
 receiver = new BluetoothReceiver(); 
 registerReceiver(receiver, filter); 
 filter=new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); 
 registerReceiver(receiver,filter); 
 } 
 
 /** 
 * 廣播 
 */ 
 private class BluetoothReceiver extends BroadcastReceiver { 
 @Override 
 public void onReceive(Context context, Intent intent) { 
 String action = intent.getAction(); 
 if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
 String str = device.getName() + "|" + device.getAddress(); 
 if (ListDevice.indexOf(str) == -1)// 防止重復添加 
  ListDevice.add(str); // 獲取設備名稱和mac地址 
 if (mAdapter != null) { 
  mAdapter.notifyDataSetChanged(); 
 } 
 showDevices(); 
 } 
 else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){ 
 progressBar.setVisibility(View.INVISIBLE); 
 show("已停止尋找"); 
 } 
 
 } 
 }; 
 /** 
 * 菜單欄點擊事件 
 * @param item 
 * @return 
 */ 
 @Override 
 public boolean onOptionsItemSelected(MenuItem item) { 
 switch (item.getItemId()){ 
 case R.id.search: 
 if(!mBtAdapter.isEnabled()){ 
  show("藍牙未開啟"); 
 } 
 else { 
  mBtAdapter.startDiscovery(); 
  show("正在尋找設備"); 
  progressBar.setVisibility(View.VISIBLE); 
 } 
 break; 
 case R.id.about: 
 Toast.makeText(MainActivity.this,"關于我們",Toast.LENGTH_SHORT).show(); 
 break; 
 default: 
 } 
 return true; 
 } 
 private void showDevices() { 
 mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, 
 ListDevice); 
 mListView.setAdapter(mAdapter); 
 } 
 /** 
 * 更新UI方法 
 * @param string 
 */ 
 private void show(final String string){ 
 runOnUiThread(new Runnable() { 
 @Override 
 public void run() { 
 status.setText(string); 
 } 
 }); 
 } 
}

 然后我的讀任務和寫任務以及連接任務是在另一個類里面實現的,也就是BlueToothTool類,這個類一個原博客是沒有寫的,只是MainActivity中用到了這個類的一些方法,但是沒有給出,所以就讓一些同學很蛋疼。我看完之后是自己補全的這個類!

package com.example.fsl.bluetooth; 
 
import android.app.Notification; 
import android.bluetooth.BluetoothDevice; 
import android.bluetooth.BluetoothSocket; 
import android.content.Context; 
import android.os.Handler; 
import android.os.Message; 
import android.support.v4.app.NotificationCompat; 
import android.util.Log; 
 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.lang.reflect.Method; 
 
import java.util.logging.LogRecord; 
 
/** 
 * Created by Fsl on 2017/12/22. 
 */ 
 
public class BlueToothTool { 
 private BluetoothDevice device; 
 private Handler mhandler; 
 BluetoothSocket socket; 
 static final int CONNECT_FAILED=1; 
 static final int CONNECT_SUCCESS=5; 
 static final int READ_FAILED=2; 
 static final int WRITE_FAILED=3; 
 static final int DATA=4; 
 private boolean isConnect=false; 
 
 public BlueToothTool(BluetoothDevice device,Handler handler){ 
 this.device=device; 
 this.mhandler=handler; 
 } 
 /** 
 *開辟連接線程任務 
 */ 
 public void connect(){ 
 Thread thread = new Thread(new Runnable() { 
 @Override 
 public void run() { 
 BluetoothSocket tmp = null; 
 Method method; 
 try { 
  method = device.getClass().getMethod("createRfcommSocket", new Class[]{int.class}); 
  tmp = (BluetoothSocket) method.invoke(device, 1); 
 } catch (Exception e) { 
  setState(CONNECT_FAILED); 
  Log.e("TAG", e.toString()); 
 } 
 socket = tmp; 
 try { 
  socket.connect(); 
  isConnect = true; 
  setState(CONNECT_SUCCESS); 
  Readtask readtask = new Readtask(); //連接成功后開啟讀取數據的線程 
  readtask.start(); 
 } catch (Exception e) { 
  setState(CONNECT_FAILED); 
  Log.e("TAG", e.toString()); 
 } 
 } 
 }); 
 new Thread(thread).start(); 
 } 
 /** 
 *開辟線程讀任務 
 */ 
 public class Readtask extends Thread{ 
 @Override 
 public void run(){ 
 byte[] buffer = new byte[1024]; 
 int bytes; 
 InputStream inputStream ; //建立輸入流讀取數據 
 while (true) { 
 try { 
  inputStream = socket.getInputStream(); 
  if ((bytes = inputStream.read(buffer)) > 0) { 
  byte[] buf_data= new byte[bytes]; 
  for (int i = 0; i < bytes; i++) { 
  buf_data[i] = buffer[i]; 
  } 
  String s = new String(buf_data); 
  Message msg = mhandler.obtainMessage(); 
  msg.what = DATA; 
  msg.obj = s; 
  mhandler.sendMessage(msg); 
  } 
 } catch (IOException e) { 
  setState(READ_FAILED); 
  Log.e("TAG", e.toString()); 
  break; 
 } 
 } 
 
 if (socket != null) { 
 try { 
  socket.close(); 
 } catch (IOException e) { 
  Log.e("TAG", e.toString()); 
 } 
 } 
 } 
 } 
 /** 
 *開辟線程寫任務 
 */ 
 public class WriteTask extends Thread{ 
 private String srt; 
 public WriteTask(String str){ 
 this.srt=str; 
 } 
 @Override 
 public void run(){ 
 OutputStream outputStream=null; 
 byte[] st=srt.getBytes(); 
 try{ 
  outputStream=socket.getOutputStream(); 
  outputStream.write(st); 
  }catch (Exception e){ 
  setState(WRITE_FAILED); 
  e.printStackTrace(); 
 } 
 } 
 
 } 
 
 
 private void setState(int mes){ 
 Message message=new Message(); 
 message.what=mes; 
 mhandler.sendMessage(message); 
 
 } 
 /** 
 *下面這個方法目前還沒有用到 
 */ 
 private byte[] getHexBytes(String message) { 
 int len = message.length() / 2; 
 char[] chars = message.toCharArray(); 
 String[] hexStr = new String[len]; 
 byte[] bytes = new byte[len]; 
 for (int i = 0, j = 0; j < len; i += 2, j++) { 
 hexStr[j] = "" + chars[i] + chars[i + 1]; 
 bytes[j] = (byte) Integer.parseInt(hexStr[j], 16); 
 } 
 return bytes; 
 } 
 
} 

以上就是我的藍牙與單片機連接通信的全過程,順便說一下,這個連接是自動連接的,不需要什么秘鑰什么的,直接搜索到HC-05藍牙直接就可以確定連接,親測有效。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

安多县| 镇江市| 子长县| 鄂托克前旗| 本溪| 泰宁县| 石林| 绵阳市| 遵化市| 玉山县| 桃园县| 常熟市| 平武县| 雅江县| 建水县| 铜陵市| 滕州市| 泗洪县| 清流县| 临汾市| 邹平县| 武平县| 阿尔山市| 高要市| 吉首市| 汝城县| 增城市| 苍山县| 涞水县| 安塞县| 兰溪市| 九寨沟县| 西峡县| 隆尧县| 家居| 桐柏县| 博客| 讷河市| 昭平县| 曲阳县| 东乌珠穆沁旗|