




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、物聯(lián)網(wǎng)應(yīng)用技術(shù)專業(yè)教學(xué)資源庫文檔文檔來源院校開發(fā)文檔編號二維碼識別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>建立一個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)牙互動是通過BluetoothAdapter類,調(diào)用getDefaultAdapter()獲得一個實(shí)例。要打開藍(lán)牙,首先我們應(yīng)該檢查是否BluetoothAdapter已經(jīng)啟用。如果不是,使用ACTION_REQUEST_ENABLE意圖調(diào)用startActivityForResult()方法。注意到startActivityForResult()方法的第二個參數(shù),是整數(shù),被設(shè)定為大于0。藍(lán)牙一個非常重要的功能是掃描和搜索,在局部區(qū)域發(fā)現(xiàn)可訪問的設(shè)備。當(dāng)我們說可發(fā)現(xiàn),我們的意思是一個設(shè)備可被啟用,它的信息是共享的可見的。要設(shè)置配對設(shè)備使用getBondedDevic
17、es(),這樣我們就可以找出所有的BluetoothDevices。考慮到startDiscovery()方法用于設(shè)備發(fā)現(xiàn)的性能問題,要獲得所發(fā)現(xiàn)的BluetoothDevices所有信息,我們應(yīng)該用ACTION_FOUND意圖注冊一個BroadcastReceiver。我們建議取消的發(fā)現(xiàn)過程,因?yàn)锽luetoothAdapter消耗很多資源,所以cancelDiscovery()用于這個目的。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等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 新希望教育2025年中考生物試題命題比賽模擬試卷(11)含解析
- 邢臺學(xué)院《診斷基本檢查一般檢查》2023-2024學(xué)年第一學(xué)期期末試卷
- 老年ERCP患者麻醉管理
- 廣東省東莞市信義校2024屆中考押題數(shù)學(xué)預(yù)測卷含解析
- 2024-2025新職工入場安全培訓(xùn)考試試題答案考點(diǎn)提分
- 2025公司主要負(fù)責(zé)人安全培訓(xùn)考試試題B卷
- 2025年企業(yè)員工崗前安全培訓(xùn)考試試題完整參考答案
- 2024-2025公司項(xiàng)目部管理人員安全培訓(xùn)考試試題黃金題型
- 2025年公司安全培訓(xùn)考試試題及參考答案(綜合題)
- 2025年公司項(xiàng)目負(fù)責(zé)人安全培訓(xùn)考試試題帶答案(黃金題型)
- 《明代染織工藝》課件
- 《品質(zhì)管理人員培訓(xùn)》課件
- 大件運(yùn)輸質(zhì)量信譽(yù)考評表
- 寧夏回族自治區(qū)勞動合同(官方范本)
- 220kv交流輸電線路金具技術(shù)規(guī)范書
- 數(shù)據(jù)中心網(wǎng)絡(luò)
- 工廠能源管理應(yīng)急預(yù)案
- 《唯物主義和唯心主義》課件(共31張)
- 競品分析知識培訓(xùn)課件
- 中小學(xué)建筑實(shí)地調(diào)研
- 工程造價畢業(yè)論文8000字
評論
0/150
提交評論