當前位置:首頁 » 安卓系統 » 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();
}

熱點內容
SQL寫序列 發布:2024-09-20 06:02:29 瀏覽:963
裝緩存下載 發布:2024-09-20 05:42:36 瀏覽:72
gon引擎自動回收腳本 發布:2024-09-20 05:39:39 瀏覽:246
好醫生連鎖店密碼多少 發布:2024-09-20 05:09:38 瀏覽:15
魔獸腳本代理 發布:2024-09-20 05:09:35 瀏覽:99
python登陸網頁 發布:2024-09-20 05:08:39 瀏覽:758
安卓qq飛車如何轉蘋果 發布:2024-09-20 04:54:30 瀏覽:178
存儲過程中in什麼意思 發布:2024-09-20 04:24:20 瀏覽:315
php顯示數據 發布:2024-09-20 03:48:38 瀏覽:501
源碼安裝軟體 發布:2024-09-20 03:44:31 瀏覽:354