




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(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ù)文檔文檔來(lái)源院校開(kāi)發(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)是通過(guò)BluetoothAdapter類,調(diào)用getDefaultAdapter()獲得一個(gè)實(shí)例。要打開(kāi)藍(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)可訪問(wèn)的設(shè)備。當(dāng)我們說(shuō)可發(fā)現(xiàn),我們的意思是一個(gè)設(shè)備可被啟用,它的信息是共享的可見(jiàn)的。要設(shè)置配對(duì)設(shè)備使用getBondedDevic
17、es(),這樣我們就可以找出所有的BluetoothDevices。考慮到startDiscovery()方法用于設(shè)備發(fā)現(xiàn)的性能問(wèn)題,要獲得所發(fā)現(xiàn)的BluetoothDevices所有信息,我們應(yīng)該用ACTION_FOUND意圖注冊(cè)一個(gè)BroadcastReceiver。我們建議取消的發(fā)現(xiàn)過(guò)程,因?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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 環(huán)保電線采購(gòu)合同協(xié)議
- 玉米收割收購(gòu)合同協(xié)議
- 瓷磚廠設(shè)備安裝合同協(xié)議
- 申請(qǐng)房子貸款合同協(xié)議
- 瓷磚鑲貼合同協(xié)議書(shū)范本
- 物流運(yùn)輸承包合同協(xié)議
- 電商競(jìng)業(yè)合同和保密協(xié)議
- 理財(cái)產(chǎn)品購(gòu)買(mǎi)合同協(xié)議
- 電梯安置房出售合同協(xié)議
- 電視活動(dòng)招商合同協(xié)議
- 電子商務(wù)的區(qū)塊鏈與加密貨幣
- DB35T 2082-2022 人民防空疏散基地建設(shè)基本要求
- 2023年中國(guó)少女發(fā)育內(nèi)衣行業(yè)發(fā)展白皮書(shū)
- 再生鋁商業(yè)計(jì)劃書(shū)
- 江蘇省蘇州市2022-2023學(xué)年二年級(jí)下學(xué)期語(yǔ)文期中調(diào)研試卷(含答案)
- 邊緣人格障礙患者辯證行為治療的療效研究
- 化學(xué)期中成績(jī)分析
- 江蘇省期末試題匯編-04-認(rèn)識(shí)圖形(二)(選擇題經(jīng)典常考題)-小學(xué)一年級(jí)數(shù)學(xué)下冊(cè)(蘇教版)
- 人力資源用工風(fēng)險(xiǎn)與防范一本通
- 用友ERPU8生產(chǎn)制造管理
- 產(chǎn)品生命周期管理培訓(xùn)
評(píng)論
0/150
提交評(píng)論