为了帮助网友解决“Android studio使用> 提取码: 626b
初涉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版本,如果是21或以下版本的话,便能确定是API版本问题,只要换成22或者以上就都可以正常运行了~ 这么坑爹的错误的确很为难初学者。 唉·········· 为这种小trick浪费很多时间真是难过。
(另外有个重要地方,别忘了给manifest里面加以下两个蓝牙 *** 作权限哦~)
<uses-permission android:name="androidpermissionBLUETOOTH"></uses-permission>
<uses-permission android:name="androidpermissionBLUETOOTH_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 = UUIDfromString("00001101-0000-1000-8000-00805F9B34FB"); //这条是蓝牙串口通用的UUID,不要更改
private static String address = "00:12:02:22:06:61"; // <==要连接的蓝牙设备MAC地址
/获得通信线路过程/
/1:获取本地BlueToothAdapter/
mBluetoothAdapter = BluetoothAdaptergetDefaultAdapter();
if(mBluetoothAdapter == null)
{
ToastmakeText(this, "Bluetooth is not available", ToastLENGTH_LONG)show();
finish();
return;
}
if(!mBluetoothAdapterisEnabled())
{
ToastmakeText(this, "Please enable your Bluetooth and re-run this program", ToastLENGTH_LONG)show();
finish();
return;
}
/2:获取远程BlueToothDevice/
BluetoothDevice device = mBluetoothAdaptergetRemoteDevice(address);
if(mBluetoothAdapter == null)
{
ToastmakeText(this, "Can't get remote device", ToastLENGTH_LONG)show();
finish();
return;
}
/3:获得Socket/
try {
btSocket = devicecreateRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
Loge(TAG, "ON RESUME: Socket creation failed", e);
}
/4:取消discovered节省资源/
mBluetoothAdaptercancelDiscovery();
/5:连接/
try {
btSocketconnect();
Loge(TAG, "ON RESUME: BT connection established, data transfer link open");
} catch (IOException e) {
try {
btSocketclose();
} catch (IOException e2) {
Log e(TAG,"ON RESUME: Unable to close socket during connection failure", e2);
}
}
/此时可以通信了,放在任意函数中/
/ try {
outStream = btSocketgetOutputStream();
inStream = btSocketgetInputStream(); //可在TextView里显示
} catch (IOException e) {
Loge(TAG, "ON RESUME: Output stream creation failed", e);
}
String message = "1";
byte[] msgBuffer = messagegetBytes();
try {
outStreamwrite(msgBuffer);
} catch (IOException e) {
Loge(TAG, "ON RESUME: Exception during write", e);
}
/
通用搜索模式代码模板:
简洁简洁方式1 demo
作用: 用VerticalSeekBar控制一个 LED屏幕的亮暗。
直接上码咯~
package comexampleseed2;
import androidappActivity;
import androidappAlertDialog;
import androidappDialog;
import androidosBundle;
import javaioIOException;
import javaioInputStream;
import javaioOutputStream;
import javautilUUID;
import androidbluetoothBluetoothAdapter;
import androidbluetoothBluetoothDevice;
import androidbluetoothBluetoothSocket;
import androidcontentDialogInterface;
import androidutilLog;
import androidviewKeyEvent;
import androidwidgetToast;
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 = UUIDfromString("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) {
superonCreate(savedInstanceState);
setContentView(Rlayoutmain);
thisvskb = (VerticalSeekBar)superfindViewById(Ridmskb);
thisvskbsetOnSeekBarChangeListener(new OnSeekBarChangeListenerX());
mBluetoothAdapter = BluetoothAdaptergetDefaultAdapter();
if(mBluetoothAdapter == null)
{
ToastmakeText(this, "Bluetooth is not available", ToastLENGTH_LONG)show();
finish();
return;
}
if(!mBluetoothAdapterisEnabled())
{
ToastmakeText(this, "Please enable your Bluetooth and re-run this program", ToastLENGTH_LONG)show();
finish();
return;
}
}
private class OnSeekBarChangeListenerX implements VerticalSeekBarOnSeekBarChangeListener {
public void onProgressChanged(VerticalSeekBar seekBar, int progress, boolean fromUser) {
//MainthiscluesetText(seekBargetProgress());
/ String message;
byte [] msgBuffer;
try {
outStream = btSocketgetOutputStream();
} catch (IOException e) {
Loge(TAG,"ON RESUME : Output Stream creation failed", e);
}
message =IntegertoString( seekBargetProgress() );
msgBuffer = messagegetBytes();
try{
outStreamwrite(msgBuffer);
} catch (IOException e) {
Loge (TAG, "ON RESUME : Exception during write", e);
} /
}
public void onStartTrackingTouch(VerticalSeekBar seekBar) {
String message;
byte [] msgBuffer;
try {
outStream = btSocketgetOutputStream();
} catch (IOException e) {
Loge(TAG,"ON RESUME : Output Stream creation failed", e);
}
message =IntegertoString( seekBargetProgress() );
msgBuffer = messagegetBytes();
try{
outStreamwrite(msgBuffer);
} catch (IOException e) {
Loge (TAG, "ON RESUME : Exception during write", e);
}
}
public void onStopTrackingTouch(VerticalSeekBar seekBar) {
String message;
byte [] msgBuffer;
try {
outStream = btSocketgetOutputStream();
} catch (IOException e) {
Loge(TAG,"ON RESUME : Output Stream creation failed", e);
}
message =IntegertoString( seekBargetProgress() );
msgBuffer = messagegetBytes();
try{
outStreamwrite(msgBuffer);
} catch (IOException e) {
Loge (TAG, "ON RESUME : Exception during write", e);
}
}
}
@Override
public void onStart()
{
superonStart();
}
@Override
public void onResume()
{
superonResume();
BluetoothDevice device = mBluetoothAdaptergetRemoteDevice(address);
try {
btSocket = devicecreateRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
Loge(TAG, "ON RESUME: Socket creation failed", e);
}
mBluetoothAdaptercancelDiscovery();
try {
btSocketconnect();
Loge(TAG, "ON RESUME: BT connection established, data transfer link open");
} catch (IOException e) {
try {
btSocketclose();
} 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 = btSocketgetOutputStream();
inStream = btSocketgetInputStream();
} catch (IOException e) {
Loge(TAG, "ON RESUME: Output stream creation failed", e);
}
String message = "read";
byte[] msgBuffer = messagegetBytes();
try {
outStreamwrite(msgBuffer);
} catch (IOException e) {
Loge(TAG, "ON RESUME: Exception during write", e);
}
int ret = -1;
while( ret != -1)
{
try {
ret = inStreamread();
} catch (IOException e)
{
eprintStackTrace();
}
}
/
}
@Override
我们先来说电脑怎么与单片机进行串口通信呢?方案1——“直连”电脑串口,不过中间需要个MAX232之类的电平转换模块;方案2——“直联”电脑USB口,不过中间需要USB转串口模块。好了,手机用哪种方案呢?也是方案2。现在Android手机只要系统在501以上,一般就支持USB OTG功能了,也就意味着可以支持Android的USB转串口。我是电子硬件工程师,也喜欢Android编程,历时一年多,做了个手机串口软件,分享给你。百度“路人软件”。
百度上搜索“串口调试助手”或者“串口调试精灵”,进行简单的安装。点击运行串口调试助手,进入相应的界面。设置串口通讯的接口,波特率校验位等相关信息。在发送栏输入要发送的信息,选择手动发送或者自动发送,这时如果没有什么错误,对方的串口通信工具就会收到发送的内容。设置接收区域的相关信息,这时对方如果发送信息,接收区就会收到相应的内容。另外也可以进行文件的发送,但是串口通信的速率过慢,传输文件的话会很耗时,不过可以用来检测通信的正确性。
以上就是关于如何使用android studio实现串口通信全部的内容,包括:如何使用android studio实现串口通信、android 串口通信、android_studio手机蓝牙串口通信源代码等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)