




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、物聯(lián)網(wǎng)應(yīng)用技術(shù)專業(yè)教學(xué)資源庫(kù)文檔文檔來源院校開發(fā)文檔編號(hào)二維碼識(shí)別2016 年 4 月25日activity_main.xml:<RelativeLayout xmlns:android=" xmlns:tools=" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity&qu
2、ot; > <TextView android:id="+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" and
3、roid:textAppearance="?android:attr/textAppearanceLarge" android:text="string/Text" /> <LinearLayout android:layout_width="match_parent"
4、60; android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="30dp" > <Button android:id="+id/turnOn" &
5、#160; android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="string/on" /> <Button android:id="+id/turnOff&
6、quot; android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="string/off" /> </LinearLayout>
7、 <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_m
8、arginTop="80dp" > <Button android:id="+id/paired" android:layout_width=&q
9、uot;wrap_content" android:layout_height="wrap_content" android:text="string/List" />
10、; <Button android:id="+id/search" android:layout_width="wrap_content" &
11、#160; android:layout_height="wrap_content" android:text="string/Find" /> <ListView
12、60; android:id="+id/listView1" android:layout_width="fill_parent" android:layout_height=&qu
13、ot;200dp" > </ListView> </LinearLayout></RelativeLayout>建立一個(gè)res/values/strings.xml用于我們菜單顯示:<?xml version="1.0" encoding="utf-8"?><resources>
14、; <string name="app_name">BluetoothTest</string> <string name="action_settings">Settings</string> <string name="Text">Status: -</string> <string name="on&
15、quot;>Turn On</string> <string name="off">Turn Off</string> <string name="List">List paired Devices</string> <string name="Find">Search new Devices / Cancel</string></r
16、esources>與藍(lán)牙互動(dòng)是通過BluetoothAdapter類,調(diào)用getDefaultAdapter()獲得一個(gè)實(shí)例。要打開藍(lán)牙,首先我們應(yīng)該檢查是否BluetoothAdapter已經(jīng)啟用。如果不是,使用ACTION_REQUEST_ENABLE意圖調(diào)用startActivityForResult()方法。注意到startActivityForResult()方法的第二個(gè)參數(shù),是整數(shù),被設(shè)定為大于0。藍(lán)牙一個(gè)非常重要的功能是掃描和搜索,在局部區(qū)域發(fā)現(xiàn)可訪問的設(shè)備。當(dāng)我們說可發(fā)現(xiàn),我們的意思是一個(gè)設(shè)備可被啟用,它的信息是共享的可見的。要設(shè)置配對(duì)設(shè)備使用getBondedDevic
17、es(),這樣我們就可以找出所有的BluetoothDevices。考慮到startDiscovery()方法用于設(shè)備發(fā)現(xiàn)的性能問題,要獲得所發(fā)現(xiàn)的BluetoothDevices所有信息,我們應(yīng)該用ACTION_FOUND意圖注冊(cè)一個(gè)BroadcastReceiver。我們建議取消的發(fā)現(xiàn)過程,因?yàn)锽luetoothAdapter消耗很多資源,所以cancelDiscovery()用于這個(gè)目的。public class MainActivity extends Activity private static final int REQUEST_ENABLE_BT =
18、1; private Button onBtn; private Button offBtn; private Button listBtn; private Button findBtn; private TextView text; private BluetoothAdapter myBluetoothAdapter; private Set<BluetoothDevice> pairedDevices;
19、60; private ListView myListView; private ArrayAdapter<String> BTArrayAdapter; Override protected void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setCo
20、ntentView(R.layout.activity_main); / take an instance of BluetoothAdapter - Bluetooth radio myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if(myBluetoothAdapter =
21、 null) onBtn.setEnabled(false); offBtn.setEnabled(false); listBtn.setEnabled(false);
22、160; findBtn.setEnabled(false); text.setText("Status: not supported");
23、; Toast.makeText(getApplicationContext(),"Your device does not support Bluetooth", Toast.LENGTH_LONG).show(); else
24、 text = (TextView) findViewById(R.id.text); onBtn = (Button)findViewById(R.id.turnOn); onBtn.setOn
25、ClickListener(new OnClickListener() Override publ
26、ic void onClick(View v) / TODO Auto-generated method stub on(v);
27、60; );
28、 offBtn = (Button)findViewById(R.id.turnOff); offBtn.setOnClickListener(new OnClickListener()
29、60; Override public void onClick(View v)
30、0; / TODO Auto-generated method stub off(v);
31、160; ); listBtn = (Button)findViewById(R.id.paired);
32、0; listBtn.setOnClickListener(new OnClickListener() Override
33、60; public void onClick(View v) / TODO Auto-generated method stub
34、0; list(v); );
35、60; findBtn = (Button)findViewById(R.id.search); findBtn.setOnClickListener(new OnClickListener()
36、 Override public void onClick(View v)
37、60; / TODO Auto-generated method stub find(v);
38、 ); myListView = (ListView)findViewById(R
39、.id.listView1); / create the arrayAdapter that contains the BTDevices, and set it to the ListView BTArrayAdapter = new Array
40、Adapter<String>(this, android.R.layout.simple_list_item_1); myListView.setAdapter(BTArrayAdapter); public void on(View view) if (!myBluetooth
41、Adapter.isEnabled() Intent turnOnIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(turnOnIntent, REQUEST_ENABLE_BT); Toas
42、t.makeText(getApplicationContext(),"Bluetooth turned on" , Toast.LENGTH_LONG).show(); else
43、; Toast.makeText(getApplicationContext(),"Bluetooth is already on", Toast.LENGTH_LONG).show();
44、 Override protected void onActivityResult(int requestCode, int resultCode, Intent data) / TODO Auto-generated method stub if(requestCode = REQUEST_ENABLE_BT)
45、160; if(myBluetoothAdapter.isEnabled() text.setText("Status: Enabled");
46、60; else text.setText("Status: Disabled"); &
47、#160; public void list(View view) / get paired devices pairedDevices = myBluetoothAdapter.
48、getBondedDevices(); / put it's one to the adapter for(BluetoothDevice device : pairedDevices) BTArrayAdapter.add(device.getName()+ &q
49、uot;n" + device.getAddress(); Toast.makeText(getApplicationContext(),"Show Paired Devices", Toast.LENGTH_SHORT).show();
50、160; final BroadcastReceiver bReceiver = new BroadcastReceiver() public void onReceive(Context context, Intent intent) St
51、ring action = intent.getAction(); / When discovery finds a device if (BluetoothDevice.ACTION_FOUND.equals(action)
52、0; / Get the BluetoothDevice object from the Intent BluetoothDevice device = intent.getParcelableEx
53、tra(BluetoothDevice.EXTRA_DEVICE); / add the name and the MAC address of the object to the arrayAdapter
54、160; BTArrayAdapter.add(device.getName() + "n" + device.getAddress(); BTArrayAdapter.notifyDataSetChanged();
55、 ; public void find(View view) if (myBluetoothAdapter.isDiscovering
56、() / the button is pressed when it discovers, so cancel the discovery myBluetoothAdapter.cancelDiscovery();
57、160; else BTArrayAdapter.clear(); &
58、#160; myBluetoothAdapter.startDiscovery(); registe
59、rReceiver(bReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND); public void off(View view) myBlueto
60、othAdapter.disable(); text.setText("Status: Disconnected"); Toast.makeText(getApplicationContext(),"Bluetooth turned off",
61、; Toast.LENGTH_LONG).show(); Override protected void onDestroy() / TODO Auto-generated method stub super.onDestroy(); unregisterReceiver(bReceiver);
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 母愛的課件文庫(kù)
- 母愛的作文課件
- 母嬰營(yíng)養(yǎng)知識(shí)護(hù)理課件
- 礦石采購(gòu)方案(3篇)
- 輸尿管腫瘤術(shù)后護(hù)理查房要點(diǎn)
- 建筑企業(yè)資金方案(3篇)
- 手機(jī)在線培訓(xùn)平臺(tái)應(yīng)用指南
- 城市地下車庫(kù)產(chǎn)權(quán)轉(zhuǎn)讓與物業(yè)管理合同
- 出租車掛靠業(yè)務(wù)車輛掛靠與維修保養(yǎng)合同范本
- 車庫(kù)租賃與停車場(chǎng)運(yùn)營(yíng)管理合同范本
- 大學(xué)英語四六級(jí)詞匯表
- 黑龍江省2024年普通高校招生體育類本科批院校專業(yè)組投檔分?jǐn)?shù)線(歷史類)
- 水閘地基施工方案
- 企業(yè)數(shù)字化轉(zhuǎn)型服務(wù)協(xié)議
- 《建立合適邊界:親子教育課件》
- DB37-T 4516-2022 高速公路邊坡光伏發(fā)電工程技術(shù)規(guī)范
- 變電所設(shè)備更換申請(qǐng)報(bào)告
- 2023年遺傳學(xué)考試題庫(kù)(含答案)
- 課題申報(bào)參考:基于多模態(tài)大數(shù)據(jù)的大學(xué)生心理危機(jī)預(yù)警機(jī)制研究
- 《消費(fèi)者行為學(xué)》教學(xué)大綱
- 《礦井扇風(fēng)機(jī)》課件
評(píng)論
0/150
提交評(píng)論