当前位置:首页 » 安卓系统 » android蓝牙连接

android蓝牙连接

发布时间: 2022-01-07 23:45:07

① android多连接蓝牙编程,据说可以同时进行7个连接

你把你的代码贴出来,在接收数据的时候是数据流接收,需要循环接收。

② android 怎么连接蓝牙设备

先展示代码结构
步骤阅读
2
连接蓝牙类
类名:MainActivity(有点偷懒,没有起表意的类名。)
步骤阅读
3
要声明的控件和变量等
步骤阅读
4
在onCreate声明控件
步骤阅读
5
ToogleButton设置开关状态
声明一个组件愿意接收
IntentFilter intent = new IntentFilter();
步骤阅读
6
BroadcastReceiver广播接收器
步骤阅读
步骤阅读
7
listview点击事件
OnItemClickListener

OnClickListener
步骤阅读
步骤阅读
8
蓝牙连接
步骤阅读
9
退出消耗页面是的onDestroy()
步骤阅读
10
布局结构图
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.bluetooth_connection.MainActivity" >
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:orientation="horizontal" >
<Button android:id="@+id/btnSearch" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="搜索" android:layout_weight="1" />
<Button android:id="@+id/btnExit" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="退出" android:layout_weight="1" />
<Button android:id="@+id/btnDis" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="开启蓝牙" android:layout_weight="1" /> </LinearLayout>
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:orientation="vertical" >
<ToggleButton android:id="@+id/tbtnSwitch" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center_horizontal" android:textOff="关闭蓝牙" android:textOn="开启蓝牙" /> </LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="0.3" android:orientation="vertical" >
<ListView android:id="@+id/lvDevices" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#f1f1f1" android:cacheColorHint="#ff333333" android:fadingEdge="none" android:scrollbars="none" > </ListView> </LinearLayout> </LinearLayout>
</RelativeLayout>

步骤阅读
11
界面效果

步骤阅读

12
例子如下

安卓系统 蓝牙 与车载蓝牙的连接

你可以试一下蓝牙连接方法大体详细步骤是:
1、首先手机、车载蓝牙设备要建立配对关系。分别开启手机、车载蓝牙设备的蓝牙功能,并将手机蓝牙设置中设为所有人可见。
2、然后在手机中搜索蓝牙设备,查找到之后选中进行配对连接,配对密码为:0000,完成配对后则连接成功。
3、车载蓝牙与手机蓝牙配对连接成功后,可以拨打和接听电话;在手机上播放音乐,可在车载蓝牙设备上欣赏音乐。
4、找到音频项找到蓝牙音频再按就是蓝牙音频装置列表,此时用手机一搜就见MB Bluetooth 了。
(3)android蓝牙连接扩展阅读:
车载蓝牙只是以无线蓝牙技术为基础而设计研发的车内无线免提系统,主要功能是为在正常行驶中用蓝牙技术与手机连接进行免提通话,以达到解放双手、降低交通肇事隐患的目的。

④ 如何让android 蓝牙连接之后断开,再自动尝试连接

手机连接蓝牙设备后会自动断开连接,建议检查:
1.查看蓝牙设备是否远离机器。
2.手机是否开启了飞行模式或超级省电模式。若开启,手机将会关闭蓝牙功能。如需使用蓝牙,需要重新开启。
3.将手机关机重启。
4.若依然自动断开蓝牙连接,建议更换其他蓝牙设备尝试。
5.若无效,请检查手机是否有系统更新推送通知。若有,将手机升级到最新版本。
6.若问题依然存在,请备份手机数据,将机器恢复出厂设置尝试。
若上述操作后手机仍然自动断开蓝牙连接,请您携带购机发票、包修卡和机器送到三星服务中心检测。

⑤ Android中如何实现蓝牙的配对与连接

蓝牙功能可以参考下面的操作打开使用:
1.打开其他设备的蓝牙,并使其对其他设备可见。
2.打开下拉顶帘,点击蓝牙图标使其变为绿色,跳出提示框,勾选对其他设备可见。
3.点击扫描,搜索到其他设备后,点击该设备名称,双方点确定后配对成功。
4.选择要传输的文件,共享通过蓝牙即可传输文件。

⑥ 如何实现android蓝牙开发 自动配对连接,并不弹出提示框

我就开始查找怎么关闭这个蓝牙配对提示框,后面还是伟大的android源码帮助了我。
在源码 BluetoothDevice 类中还有两个隐藏方法
cancelBondProcess()和cancelPairingUserInput()
这两个方法一个是取消配对进程一个是取消用户输入
下面是自动配对的代码
Mainfest,xml注册

<receiverandroid:name=".">

<intent-filter>

<actionandroid:name="android.bluetooth.device.action.PAIRING_REQUEST"/>

</intent-filter>

</receiver>

自己在收到广播时处理并将预先输入的密码设置进去

java">
{

StringstrPsw="0";

@Override
publicvoidonReceive(Contextcontext,Intentintent)
{
//TODOAuto-generatedmethodstub
if(intent.getAction().equals(
"android.bluetooth.device.action.PAIRING_REQUEST"))
{
BluetoothDevicebtDevice=intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

//byte[]pinBytes=BluetoothDevice.convertPinToBytes("1234");
//device.setPin(pinBytes);
Log.i("tag11111","ddd");
try
{
ClsUtils.setPin(btDevice.getClass(),btDevice,strPsw);//手机和蓝牙采集器配对
ClsUtils.createBond(btDevice.getClass(),btDevice);
ClsUtils.cancelPairingUserInput(btDevice.getClass(),btDevice);
}
catch(Exceptione)
{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}


}
}
<b>/************************************蓝牙配对函数***************/
importjava.lang.reflect.Field;
importjava.lang.reflect.Method;

importandroid.bluetooth.BluetoothDevice;
importandroid.util.Log;
publicclassClsUtils
{

/**
*与设备配对参考源码:platform/packages/apps/Settings.git
*/Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java
*/
staticpublicbooleancreateBond(ClassbtClass,BluetoothDevicebtDevice)
throwsException
{
MethodcreateBondMethod=btClass.getMethod("createBond");
BooleanreturnValue=(Boolean)createBondMethod.invoke(btDevice);
returnreturnValue.booleanValue();
}

/**
*与设备解除配对参考源码:platform/packages/apps/Settings.git
*/Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java
*/
staticpublicbooleanremoveBond(ClassbtClass,BluetoothDevicebtDevice)
throwsException
{
MethodremoveBondMethod=btClass.getMethod("removeBond");
BooleanreturnValue=(Boolean)removeBondMethod.invoke(btDevice);
returnreturnValue.booleanValue();
}

staticpublicbooleansetPin(ClassbtClass,BluetoothDevicebtDevice,
Stringstr)throwsException
{
try
{
MethodremoveBondMethod=btClass.getDeclaredMethod("setPin",
newClass[]
{byte[].class});
BooleanreturnValue=(Boolean)removeBondMethod.invoke(btDevice,
newObject[]
{str.getBytes()});
Log.e("returnValue",""+returnValue);
}
catch(SecurityExceptione)
{
//thrownewRuntimeException(e.getMessage());
e.printStackTrace();
}
catch(IllegalArgumentExceptione)
{
//thrownewRuntimeException(e.getMessage());
e.printStackTrace();
}
catch(Exceptione)
{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
returntrue;

}

//取消用户输入
(ClassbtClass,
BluetoothDevicedevice)

throwsException
{
MethodcreateBondMethod=btClass.getMethod("cancelPairingUserInput");
//cancelBondProcess()
BooleanreturnValue=(Boolean)createBondMethod.invoke(device);
returnreturnValue.booleanValue();
}

//取消配对
(ClassbtClass,
BluetoothDevicedevice)

throwsException
{
MethodcreateBondMethod=btClass.getMethod("cancelBondProcess");
BooleanreturnValue=(Boolean)createBondMethod.invoke(device);
returnreturnValue.booleanValue();
}

/**
*
*@paramclsShow
*/
(ClassclsShow)
{
try
{
//取得所有方法
Method[]hideMethod=clsShow.getMethods();
inti=0;
for(;i<hideMethod.length;i++)
{
Log.e("methodname",hideMethod[i].getName()+";andtheiis:"
+i);
}
//取得所有常量
Field[]allFields=clsShow.getFields();
for(i=0;i<allFields.length;i++)
{
Log.e("Fieldname",allFields[i].getName());
}
}
catch(SecurityExceptione)
{
//thrownewRuntimeException(e.getMessage());
e.printStackTrace();
}
catch(IllegalArgumentExceptione)
{
//thrownewRuntimeException(e.getMessage());
e.printStackTrace();
}
catch(Exceptione)
{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}</b>
执行时直接使用:
<b>publicstaticbooleanpair(StringstrAddr,StringstrPsw)
{
booleanresult=false;
=BluetoothAdapter
.getDefaultAdapter();

bluetoothAdapter.cancelDiscovery();

if(!bluetoothAdapter.isEnabled())
{
bluetoothAdapter.enable();
}

if(!BluetoothAdapter.checkBluetoothAddress(strAddr))
{//检查蓝牙地址是否有效

Log.d("mylog","devAdneffient!");
}

BluetoothDevicedevice=bluetoothAdapter.getRemoteDevice(strAddr);

if(device.getBondState()!=BluetoothDevice.BOND_BONDED)
{
try
{
Log.d("mylog","NOTBOND_BONDED");
ClsUtils.setPin(device.getClass(),device,strPsw);//手机和蓝牙采集器配对
ClsUtils.createBond(device.getClass(),device);
remoteDevice=device;//配对完毕就把这个设备对象传给全局的remoteDevice
result=true;
}
catch(Exceptione)
{
//TODOAuto-generatedcatchblock

Log.d("mylog","setPiNfailed!");
e.printStackTrace();
}//

}
else
{
Log.d("mylog","HASBOND_BONDED");
try
{
ClsUtils.createBond(device.getClass(),device);
ClsUtils.setPin(device.getClass(),device,strPsw);//手机和蓝牙采集器配对
ClsUtils.createBond(device.getClass(),device);
remoteDevice=device;//如果绑定成功,就直接把这个设备对象传给全局的remoteDevice
result=true;
}
catch(Exceptione)
{
//TODOAuto-generatedcatchblock
Log.d("mylog","setPiNfailed!");
e.printStackTrace();
}
}
returnresult;
}</b>

⑦ Android开发 蓝牙连接问题

Android 蓝牙编程的基本步骤:
1.获取蓝牙适配器BluetoothAdapter blueadapter=BluetoothAdapter.getDefaultAdapter();
如果BluetoothAdapter 为null,说明android手机没有蓝牙模块。
判断蓝牙模块是否开启,blueadapter.isEnabled() true表示已经开启,false表示蓝牙并没启用。
2.启动配置蓝牙可见模式,即进入可配对模式Intent in=new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
in.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 200);
startActivity(in); ,200就表示200秒。
3.获取蓝牙适配器中已经配对的设备Set<BluetoothDevice> device=blueadapter.getBondedDevices();
4.还需要在androidManifest.xml中声明蓝牙的权限
<uses-permission android:name="android.permission.BLUETOOTH" />

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
接下来就是根据自己的需求对BluetoothAdapter 的操作了。

⑧ android如何实现一台手机通过蓝牙连另一台

手机之间通过蓝牙连接传送文件,请参考以下步骤:
1、双方手机开启蓝牙开关,路径:设置--常规--蓝牙--开启开关;
2、开启开放检测开关,开启后才可以被附近所有蓝牙设备检测到;
3、扫描到需连接的蓝牙设备,点击连接;
4、双方手机提示蓝牙配对请求和配对型号,双方手机点击配对;
5、配对成功,已配对的设备里面会显示连接成功的蓝牙设备;
6、打开文件,选择蓝牙发送和需发送到的蓝牙设备,接收文件即可。

⑨ android蓝牙编程-如何实现自动连接上次连接过的蓝牙从机模块

每次只连接一个,传完数据后,连接下一个.要多连接的话,直接创建无线网络来连接.蓝牙能连接7个是因为只能保存蓝牙只能保存匹配数是7个

⑩ 如何实现android蓝牙自动配对连接

android蓝牙自动配对连接的具体代码如下:
1. 获取蓝牙适配器BluetoothAdapter blueadapter=BluetoothAdapter.getDefaultAdapter();
如果BluetoothAdapter 为null,说明android手机没有蓝牙模块。
2. 判断蓝牙模块是否开启,blueadapter.isEnabled() true表示已经开启,false表示蓝牙并没启用。
3. 启动配置蓝牙可见模式,即进入可配对模式Intent in=new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
in.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 200);
startActivity(in); ,200就表示200秒。
4. 获取蓝牙适配器中已经配对的设备Set<BluetoothDevice> device=blueadapter.getBondedDevices();
当然,还需要在androidManifest.xml中声明蓝牙的权限
<uses-permission android:name="android.permission.BLUETOOTH" />

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
5.自动配对设置Pin值

static public boolean autoBond(Class btClass, BluetoothDevice device, String strPin)
throws Exception {
Method autoBondMethod = btClass.getMethod("setPin", new Class[] { byte[].class });
Boolean result = (Boolean) autoBondMethod
.invoke(device, new Object[] { strPin.getBytes() });
return result;
}

6.开始配对请求
static public boolean createBond(Class btClass, BluetoothDevice device) throws Exception {
Method createBondMethod = btClass.getMethod("createBond");
Boolean returnValue = (Boolean) createBondMethod.invoke(device);
return returnValue.booleanValue();
}

热点内容
好医生连锁店密码多少 发布:2024-09-20 05:09:38 浏览:14
魔兽脚本代理 发布:2024-09-20 05:09:35 浏览:96
python登陆网页 发布:2024-09-20 05:08:39 浏览:755
安卓qq飞车如何转苹果 发布:2024-09-20 04:54:30 浏览:177
存储过程中in什么意思 发布:2024-09-20 04:24:20 浏览:314
php显示数据 发布:2024-09-20 03:48:38 浏览:499
源码安装软件 发布:2024-09-20 03:44:31 浏览:353
入门编程游戏的书 发布:2024-09-20 03:31:26 浏览:235
e盒的算法 发布:2024-09-20 03:30:52 浏览:143
win10登录密码如何修改登录密码 发布:2024-09-20 03:09:43 浏览:70