初涉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
#include <reg51.h>#include <intrins.h>
#define uchar unsigned char
uchar data tab[10]//将要提取的数据装入这个数组,数组元局核素为10个,信腊租不够时自己改一下
main()
{
uchar i
SCON = 0x50
TMOD = 0x20
TH1 = 0x??//串口波特率设置,需要提供单片机的时钟频率。
TL1 = 0x??
TR1 = 1
while(1)
{
for(i = 0i<8i++)//等待串口发送的前8个字节数据并丢弃,这是最简单方法,当然也有复杂的
{
while(!RI)//等待接收串口数据
RI =0//清接收标志
}
for(i = 0i<10i++)
{
while(!RI)
tab[i] = SBUF//将提起的数据载入数组
RI = 0
if(!SBUF)brwak//如果接收的数据为00,结束提取,通过变量“i”可知道所提取滑兆数据的字节数
}
for(i = 0i<4i++ )//等待最后4个字节接收并丢弃数据
{
while(!RI)
RI = 0
}
}
}
该插件可以通过蓝牙进行串行通信。 它是为了在Android或iOS和Arduino之间进行通信而编写的。
Repo(备用): https://github.com/don/BluetoothSerial
1.安装Cordova和Ionic原生插件:
2. 添加插件到app's module
Android
iOS
Windows Phone 8
connect(macAddress_or_uuid)
Platforms:****ANDROID IOS WINDOWS PHONE
连接到蓝牙设备
****Returns:** Observable<any>
连接订阅,断开连接取消订阅。
connectInsecure(macAddress)
Platforms:****ANDROID
蓝牙设备不安全地连接
****Returns:** Observable<any>
连接订阅,断开连接取消订阅。
disconnect()
断开连接的设备
****Returns:** Promise<any>
write(data)
Platforms:****ANDROID IOS WINDOWS PHONE
将数据写入串口
****Returns:** Promise<any>
当数据写入时返回promise
available()
Platforms:****ANDROID IOS WINDOWS PHONE
获取可用数据的字节数
****Returns:** Promise<any>
返回包含可用字节的promise
read()
Platforms:****ANDROID IOS WINDOWS PHONE
从缓冲区读取数据
****Returns:** Promise<any>
从缓冲区返回数据的promise
readUntil(delimiter)
Platforms:****ANDROID IOS WINDOWS PHONE
从缓冲区读取数据,直到达到定界符
****Returns:** Promise<any>
returns a promise
subscribe(delimiter)
Platforms:****ANDROID IOS WINDOWS PHONE
订阅收到数据时收到通知
****Returns:** Observable<any>
返回一个observable.
subscribeRawData()
Platforms:****ANDROID IOS WINDOWS PHONE
订阅收到数仿前据时收到通知
****Returns:** Observable<any>
返回皮圆一个observable
clear()
Platforms:****ANDROID IOS WINDOWS PHONE
清除缓冲区中的数据
****Returns:** Promise<any>
完成后返回promise
list()
Platforms:****ANDROID IOS WINDOWS PHONE
列出绑定设备
****Returns:** Promise<any>
返回一个promise
isEnabled()
Platforms:****ANDROID IOS WINDOWS PHONE
报备握清告蓝牙是否启用
****Returns:** Promise<any>
返回一个promise
isConnected()
Platforms:****ANDROID IOS WINDOWS PHONE
报告连接状态
****Returns:** Promise<any>
返回一个promise
readRSSI()
Platforms:****ANDROID IOS WINDOWS PHONE
从连接的外围设备读取RSSI
****Returns:** Promise<any>
返回一个promise
showBluetoothSettings()
Platforms:****ANDROID IOS WINDOWS PHONE
显示设备上的蓝牙设置
****Returns:** Promise<any>
返回一个promise
enable()
Platforms:****ANDROID IOS WINDOWS PHONE
在设备上启用蓝牙
****Returns:** Promise<any>
返回一个 promise
discoverUnpaired()
Platforms:****ANDROID IOS WINDOWS PHONE
发现不配对的设备
****Returns:** Promise<any>
返回一个promise
setDeviceDiscoveredListener()
Platforms:****ANDROID IOS WINDOWS PHONE
订阅蓝牙设备发现通知。 发现过程必须通过discoverUnpaired 来启动功能。
****Returns:** Observable<any>
返回一个 observable
setName(newName)
Platforms:****ANDROID
设置广播到其他设备的可读设备名称
setDiscoverable(discoverableDuration)
Platforms:****ANDROID
使设备可以被其他设备发现
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)