SimpleBluetooth.zip

  • PUDN用户
    了解作者
  • JavaScript
    开发工具
  • 56KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 14
    下载次数
  • 2013-06-26 21:11
    上传日期
该案例提供了使用布局设计用户界面的方法,包括Basic Layout,List Layout, Grid Layout, Tab Layout, Adapters,Styles,Gallery,Dialog, Drawer,适用于初学者
SimpleBluetooth.zip
内容介绍
package com.androidbook.simplebluetooth; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.HashMap; import java.util.Set; import java.util.UUID; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothServerSocket; import android.bluetooth.BluetoothSocket; import android.content.BroadcastReceiver; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.media.MediaPlayer; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.TextView; import android.widget.ToggleButton; import android.widget.AdapterView.OnItemClickListener; public class SimpleBluetooth extends Activity { private static final String SIMPLE_BT_RESPONSE_POLO = "polo"; private static final String SIMPLE_BT_COMMAND_MARCO = "marco"; private static final String SIMPLE_BT_COMMAND_PING = "ping"; private static final int BT_DISCOVERABLE_DURATION = 300; // max duration, useful for debuggin //120; private static final String DEBUG_TAG = "SimpleBluetooth"; // generated randomly online; search for "web uuid generate"; and then edited slightly, keep in mind it's hex private static final UUID SIMPLE_BT_APP_UUID = UUID.fromString("0dfb786a-cafe-feed-cafe-982fdfe4bcbf"); private static final String SIMPLE_BT_NAME = "SimpleBT"; // dialog id private static final int DEVICE_PICKER_DIALOG = 1001; // intercom with threads private final Handler handler = new Handler(); private BluetoothAdapter btAdapter; private BtReceiver btReceiver; private ServerListenThread serverListenThread; private ClientConnectThread clientConnectThread; private BluetoothDataCommThread bluetoothDataCommThread; private BluetoothDevice remoteDevice; private BluetoothSocket activeBluetoothSocket; // for sound private MediaPlayer player; private HashMap<String, BluetoothDevice> discoveredDevices = new HashMap<String, BluetoothDevice>(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btAdapter = BluetoothAdapter.getDefaultAdapter(); if (btAdapter == null) { // no bluetooth available on device setStatus("No bluetooth available. :("); disableAllButtons(); } else { setStatus("Bluetooth available! :)"); // we need a broadcast receiver now btReceiver = new BtReceiver(); // register for state change broadcast events IntentFilter stateChangedFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); this.registerReceiver(btReceiver, stateChangedFilter); // register for discovery events IntentFilter actionFoundFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND); registerReceiver(btReceiver, actionFoundFilter); // check current state int currentState = btAdapter.getState(); setUIForBTState(currentState); if (currentState == BluetoothAdapter.STATE_ON) { findDevices(); } } } private void findDevices() { String lastUsedRemoteDevice = getLastUsedRemoteBTDevice(); if (lastUsedRemoteDevice != null) { setStatus("Checking for known paired devices, namely: "+lastUsedRemoteDevice); // see if this device is in a list of currently visible (?), paired devices Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevices(); for (BluetoothDevice pairedDevice : pairedDevices) { if (pairedDevice.getAddress().equals(lastUsedRemoteDevice)) { setStatus("Found device: " + pairedDevice.getName() + "@" + lastUsedRemoteDevice); remoteDevice = pairedDevice; } } } if (remoteDevice == null) { setStatus("Starting discovery..."); // start discovery if (btAdapter.startDiscovery()) { setStatus("Discovery started..."); } } // also set discoverable setStatus("Enabling discoverable, user will see dialog..."); Intent discoverMe = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); discoverMe.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, BT_DISCOVERABLE_DURATION); startActivity(discoverMe); // also start listening for connections setStatus("Enabling listening socket thread"); serverListenThread = new ServerListenThread(); serverListenThread.start(); } private String getLastUsedRemoteBTDevice() { SharedPreferences prefs = getPreferences(MODE_PRIVATE); String result = prefs.getString("LAST_REMOTE_DEVICE_ADDRESS", null); return result; } private void setLastUsedRemoteBTDevice(String name) { SharedPreferences prefs = getPreferences(MODE_PRIVATE); Editor edit = prefs.edit(); edit.putString("LAST_REMOTE_DEVICE_ADDRESS", name); edit.commit(); } private void disableAllButtons() { Button button; int[] buttonIds = { R.id.bt_toggle, R.id.connect, R.id.marco, R.id.ping }; for (int buttonId : buttonIds) { button = (Button) findViewById(buttonId); button.setEnabled(false); } } private ToggleButton btToggle; private void setUIForBTState(int state) { if (btToggle == null) { btToggle = (ToggleButton) findViewById(R.id.bt_toggle); } switch (state) { case BluetoothAdapter.STATE_ON: btToggle.setChecked(true); btToggle.setEnabled(true); setStatus("BT state now on"); break; case BluetoothAdapter.STATE_OFF: btToggle.setChecked(false); btToggle.setEnabled(true); setStatus("BT state now off"); break; case BluetoothAdapter.STATE_TURNING_OFF: btToggle.setChecked(true); btToggle.setEnabled(false); setStatus("BT state turning off"); break; case BluetoothAdapter.STATE_TURNING_ON: btToggle.setChecked(false); btToggle.setEnabled(false); setStatus("BT state turning on"); break; } } private TextView statusField; private void setStatus(String string) { if (statusField == null) { statusField = (TextView) findViewById(R.id.output_display); } String current = (String) statusField.getText(); current = string + "\n" + current; // don't let it get too long if (current.length() > 1500) { int truncPoint = current.lastIndexOf("\n"); current = (String) current.subSequence(0, truncPoint); } statusField.setText(current); } public void doToggleBT(View view) { Log.v(DEBUG_TAG, "doToggleBT() called"); if (btToggle == null) { btToggle = (ToggleButton) findViewById(R.id.bt_toggle); } // check the new state of the button to see what the
评论
    相关推荐
    • tab.rar
      基于CSS3+JQuery制作实现的一款简单的Tab选项卡,采用圆滑边角的风格设计,既简洁又漂亮,且兼容IE8及以上浏览器,有需要网页选项卡的朋友不妨下载本源码。
    • tab.zip
      javascript仿浏览器页面标签切换的TAB选项卡代码
    • Tab.zip
      Tab 标签切换,可左右滑动切换视图。简单的方法实现的Tab配合ViewPager滑动。
    • TAB.rar
      CXTabCtrl an easier tab control for dialogs and forms
    • Tab.zip
      简单使用VC中的Tab控件,简单大方,思路清晰,看过之后,很快就会使用
    • tab.zip
      点击 TabBar 上的“更多”按钮时,会出现另外一条TabBar,显示更多隐藏的按钮。可用于TabBar上按钮过多的情况。
    • tab切换效果.rar
      android 页面切换例子(tab切换效果)
    • tab.rar
      tab栏切换效果,基于bootstrap,jq
    • TAB.rar
      在同一个页面应用多种TAB的综合实例,可在一个页面中同时布局选项卡,也布局滑动门,或者是二者兼顾。这个问题以前用户就提到过,不知如何在同一页面布置多个选项卡,你可以参考一下这个。  1、支持一个页面多次...
    • TAB.rar
      完成不同功能的网页Tab,滑动门和选项卡,选项卡功能和使用说明:  1、支持一个页面多次引用;  2、一次引用实现多次切换;  3、点击/滑过等切换方式自定义;  4、自动播放+鼠标动作切换;  5、自动...