at蓝牙安卓源码(安卓蓝牙开发源码)
本文目录一览:
- 1、android 怎么通过蓝牙向一个硬件发送AT指令
- 2、谁有android蓝牙与电脑蓝牙之间的通信源码啊,或者只要一端装了apk也行。
- 3、android_studio手机蓝牙串口通信源代码
- 4、Android源码中有关蓝牙配对的程序在哪些文件中?
android 怎么通过蓝牙向一个硬件发送AT指令
通过BluetoothSocketat蓝牙安卓源码的OutputStream发送过去后,InputStream获取不到数据...代码用at蓝牙安卓源码的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)
谁有android蓝牙与电脑蓝牙之间的通信源码啊,或者只要一端装了apk也行。
官方sdk自带一个蓝牙例子at蓝牙安卓源码,自己找吧at蓝牙安卓源码,名字就叫bluetooth
android_studio手机蓝牙串口通信源代码
初涉android的蓝牙操作,按照固定MAC地址连接获取Device时,程序始终是异常终止,查了好多天代码都没查出原因。今天改了一下API版本,突然就成功连接了。总结之后发现果然是个坑爹之极的错误。
为了这种错误拼命查原因浪费大把时间是非常不值得的,但是问题不解决更是揪心。可惜我百度了那么多,都没有给出确切原因。今天特此mark,希望后来者遇到这个问题的时候能轻松解决。
下面是我的连接过程,中间崩溃原因及解决办法。
1:用AT指令获得蓝牙串口的MAC地址,地址是简写的,按照常理猜测可得标准格式。
2:开一个String adress= "************" //MAC地址, String MY_UUID= "************"//UUID根据通信而定,网上都有。
3:取得本地Adapter用getDefaultAdapter(); 远程的则用getRemoteDevice(adress); 之后便可用UUID开socket进行通信。
如果中途各种在getRemoteDevice处崩溃,大家可以查看一下当前的API版本,如果是2.1或以下版本的话,便能确定是API版本问题,只要换成2.2或者以上就都可以正常运行了~ 这么坑爹的错误的确很为难初学者。 唉·········· 为这种小trick浪费很多时间真是难过。
(另外有个重要地方,别忘了给manifest里面加以下两个蓝牙操作权限哦~)
uses-permission android:name="android.permission.BLUETOOTH"/uses-permission
uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/uses-permission
下面附上Android蓝牙操作中用固定MAC地址传输信息的模板,通用搜索模式日后再补删模板:
private BluetoothAdapter mBluetoothAdapter = null;
private BluetoothSocket btSocket = null;
private OutputStream outStream = null;
private InputStream inStream = null;
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); //这条是蓝牙串口通用的UUID,不要更改
private static String address = "00:12:02:22:06:61"; // ==要连接的蓝牙设备MAC地址
/*获得通信线路过程*/
/*1:获取本地BlueToothAdapter*/
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter == null)
{
Toast.makeText(this, "Bluetooth is not available.", Toast.LENGTH_LONG).show();
finish();
return;
}
if(!mBluetoothAdapter.isEnabled())
{
Toast.makeText(this, "Please enable your Bluetooth and re-run this program.", Toast.LENGTH_LONG).show();
finish();
return;
}
/*2:获取远程BlueToothDevice*/
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
if(mBluetoothAdapter == null)
{
Toast.makeText(this, "Can't get remote device.", Toast.LENGTH_LONG).show();
finish();
return;
}
/*3:获得Socket*/
try {
btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
Log.e(TAG, "ON RESUME: Socket creation failed.", e);
}
/*4:取消discovered节省资源*/
mBluetoothAdapter.cancelDiscovery();
/*5:连接*/
try {
btSocket.connect();
Log.e(TAG, "ON RESUME: BT connection established, data transfer link open.");
} catch (IOException e) {
try {
btSocket.close();
} catch (IOException e2) {
Log .e(TAG,"ON RESUME: Unable to close socket during connection failure", e2);
}
}
/*此时可以通信了,放在任意函数中*/
/* try {
outStream = btSocket.getOutputStream();
inStream = btSocket.getInputStream(); //可在TextView里显示
} catch (IOException e) {
Log.e(TAG, "ON RESUME: Output stream creation failed.", e);
}
String message = "1";
byte[] msgBuffer = message.getBytes();
try {
outStream.write(msgBuffer);
} catch (IOException e) {
Log.e(TAG, "ON RESUME: Exception during write.", e);
}
*/
通用搜索模式代码模板:
简洁简洁方式1 demo
作用: 用VerticalSeekBar控制一个 LED屏幕的亮暗。
直接上码咯~
package com.example.seed2;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.DialogInterface;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;
public class MetalSeed extends Activity {
private static final String TAG = "BluetoothTest";
private BluetoothAdapter mBluetoothAdapter = null;
private BluetoothSocket btSocket = null;
private OutputStream outStream = null;
private InputStream inStream = null;
private VerticalSeekBar vskb = null;
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); //这条是蓝牙串口通用的UUID,不要更改
private static String address = "00:12:02:22:06:61"; // ==要连接的蓝牙设备MAC地址
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.vskb = (VerticalSeekBar)super.findViewById(R.id.mskb);
this.vskb.setOnSeekBarChangeListener(new OnSeekBarChangeListenerX());
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter == null)
{
Toast.makeText(this, "Bluetooth is not available.", Toast.LENGTH_LONG).show();
finish();
return;
}
if(!mBluetoothAdapter.isEnabled())
{
Toast.makeText(this, "Please enable your Bluetooth and re-run this program.", Toast.LENGTH_LONG).show();
finish();
return;
}
}
private class OnSeekBarChangeListenerX implements VerticalSeekBar.OnSeekBarChangeListener {
public void onProgressChanged(VerticalSeekBar seekBar, int progress, boolean fromUser) {
//Main.this.clue.setText(seekBar.getProgress());
/* String message;
byte [] msgBuffer;
try {
outStream = btSocket.getOutputStream();
} catch (IOException e) {
Log.e(TAG,"ON RESUME : Output Stream creation failed.", e);
}
message =Integer.toString( seekBar.getProgress() );
msgBuffer = message.getBytes();
try{
outStream.write(msgBuffer);
} catch (IOException e) {
Log.e (TAG, "ON RESUME : Exception during write.", e);
} */
}
public void onStartTrackingTouch(VerticalSeekBar seekBar) {
String message;
byte [] msgBuffer;
try {
outStream = btSocket.getOutputStream();
} catch (IOException e) {
Log.e(TAG,"ON RESUME : Output Stream creation failed.", e);
}
message =Integer.toString( seekBar.getProgress() );
msgBuffer = message.getBytes();
try{
outStream.write(msgBuffer);
} catch (IOException e) {
Log.e (TAG, "ON RESUME : Exception during write.", e);
}
}
public void onStopTrackingTouch(VerticalSeekBar seekBar) {
String message;
byte [] msgBuffer;
try {
outStream = btSocket.getOutputStream();
} catch (IOException e) {
Log.e(TAG,"ON RESUME : Output Stream creation failed.", e);
}
message =Integer.toString( seekBar.getProgress() );
msgBuffer = message.getBytes();
try{
outStream.write(msgBuffer);
} catch (IOException e) {
Log.e (TAG, "ON RESUME : Exception during write.", e);
}
}
}
@Override
public void onStart()
{
super.onStart();
}
@Override
public void onResume()
{
super.onResume();
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
try {
btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
Log.e(TAG, "ON RESUME: Socket creation failed.", e);
}
mBluetoothAdapter.cancelDiscovery();
try {
btSocket.connect();
Log.e(TAG, "ON RESUME: BT connection established, data transfer link open.");
} catch (IOException e) {
try {
btSocket.close();
} catch (IOException e2) {
Log .e(TAG,"ON RESUME: Unable to close socket during connection failure", e2);
}
}
// Create a data stream so we can talk to server.
/* try {
outStream = btSocket.getOutputStream();
inStream = btSocket.getInputStream();
} catch (IOException e) {
Log.e(TAG, "ON RESUME: Output stream creation failed.", e);
}
String message = "read";
byte[] msgBuffer = message.getBytes();
try {
outStream.write(msgBuffer);
} catch (IOException e) {
Log.e(TAG, "ON RESUME: Exception during write.", e);
}
int ret = -1;
while( ret != -1)
{
try {
ret = inStream.read();
} catch (IOException e)
{
e.printStackTrace();
}
}
*/
}
@Override
Android源码中有关蓝牙配对的程序在哪些文件中?
你是用的蓝牙2.0,还是4.0 BLE?不管是这两者中哪一个,android SDK都有相关sample可以学习使用,从搜索设备,建立连接,到两端的通信都有,基本覆盖我们要使用的所有功能。
2.0的sample是:BluetoothChat,里边有3个.java文件,网上对它的相关讲解很多,你可以参考blog.csdn.net/homebei2/article/details/6078007;
4.0的sample是:BluetoothLeGatt,如果我没记错的话,里边有4个.java文件,因为google在android4.3才支持BLE的api,相对来说比较新,用的话主要是理解一些新的协议栈概念,不是很难,网上资料相对来说少些,你也可以找找看