找传奇、传世资源到传世资源站!

Android 蓝牙与单片机交互 实例

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

Android例子源码安卓通过蓝牙与单片机交互
from clipboard
/* * Copyright (C) 2009 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.example.android.BluetoothLock;import java.util.Hashtable;import com.example.android.BluetoothLock.R;import android.annotation.SuppressLint;import android.annotation.TargetApi;import android.app.Activity;import android.app.AlertDialog;import android.bluetooth.BluetoothAdapter;import android.bluetooth.BluetoothDevice;import android.content.Intent;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.util.Log;import android.view.KeyEvent;import android.view.Menu;import android.view.MenuInflater;import android.view.MenuItem;import android.view.View;import android.view.Window;import android.view.View.OnClickListener;import android.view.inputmethod.EditorInfo;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.EditText;import android.widget.ListView;import android.widget.TextView;import android.widget.Toast;/** * This is the main Activity that displays the current Lock session. */public class BluetoothLock extends Activity { // Debugging private static final String TAG = "BluetoothLock"; private static final boolean D = true; // Message types sent from the BluetoothLockService Handler public static final int MESSAGE_STATE_CHANGE = 1; public static final int MESSAGE_READ = 2; public static final int MESSAGE_WRITE = 3; public static final int MESSAGE_DEVICE_NAME = 4; public static final int MESSAGE_TOAST = 5; //设定从机模块返回的数据 public static final String OPEN_LOCK_SUCCESS = "S"; public static final String OPEN_LOCK_FAIL = "F"; // Key names received from the BluetoothLockService Handler public static final String DEVICE_NAME = "device_name"; public static final String TOAST = "toast"; // Intent request codes private static final int REQUEST_CONNECT_DEVICE = 1; private static final int REQUEST_ENABLE_BT = 2; // Layout Views TextView mTitle; private EditText mOutEditText; private Button mSendButton; private TextView mSetPassword; // Name of the connected device private String mConnectedDeviceName = null; // String buffer for outgoing messages private StringBuffer mOutStringBuffer; // Local Bluetooth adapter private BluetoothAdapter mBluetoothAdapter = null; // Member object for the Lock services private BluetoothLockService mLockService = null; @SuppressLint("NewApi")@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if(D) Log.e(TAG, " ON CREATE "); // Set up the window layout requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.main); getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title); // Set up the custom title mTitle = (TextView) findViewById(R.id.title_left_text); mTitle.setText(R.string.app_name); mTitle = (TextView) findViewById(R.id.title_right_text); //先进行连接然后才能修改密码 /** * 修改密码 */ //添加点击“修改密码”事件 mSetPassword = (TextView) findViewById(R.id.text_setPassword); mSetPassword.setOnClickListener(new OnClickListener(){@Override //激活修改密码窗体public void onClick(View v) { Intent intent = new Intent(BluetoothLock.this,SetPasswordActivity.class); //Intent intent = new Intent(BluetoothLock.this,otherActivity.class); startActivity(intent); } }); // Get local Bluetooth adapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); // If the adapter is null, then Bluetooth is not supported if (mBluetoothAdapter == null) { Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show(); finish(); return; } } @SuppressLint("NewApi")@Override public void onStart() { super.onStart(); if(D) Log.e(TAG, " ON START "); // If BT is not on, request that it be enabled. // setupLock() will then be called during onActivityResult if (!mBluetoothAdapter.isEnabled()) { Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableIntent, REQUEST_ENABLE_BT); // Otherwise, setup the Lock session } else { if (mLockService == null) setupLock(); } } @Override public synchronized void onResume() { super.onResume(); if(D) Log.e(TAG, " ON RESUME "); // Performing this check in onResume() covers the case in which BT was // not enabled during onStart(), so we were paused to enable it... // onResume() will be called when ACTION_REQUEST_ENABLE activity returns. if (mLockService != null) { // Only if the state is STATE_NONE, do we know that we haven't started already if (mLockService.getState() == BluetoothLockService.STATE_NONE) { // Start the Bluetooth Lock services mLockService.start(); } } } @SuppressLint("NewApi")private void setupLock() { Log.d(TAG, "setupLock()"); // Initialize the compose field with a listener for the return key mOutEditText = (EditText) findViewById(R.id.setCurrentPassword); mOutEditText.setOnEditorActionListener(mWriteListener); // Initialize the send button with a listener that for click events mSendButton = (Button) findViewById(R.id.button_send); mSendButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { // Send a message using content of the edit text widget TextView view = (TextView) findViewById(R.id.setCurrentPassword); String message = view.getText().toString();//获取用户输入的内容 //对用户的数据进行处理 message = "&" message "$"; sendMessage(message); } }); // Initialize the BluetoothLockService to perform bluetooth connections mLockService = new BluetoothLockService(this, mHandler); // Initialize the buffer for outgoing messages mOutStringBuffer = new StringBuffer(""); } @Override public synchronized void onPause() { super.onPause(); if(D) Log.e(TAG, "- ON PAUSE -"); } @Override public void onStop() { super.onStop(); if(D) Log.e(TAG, "-- ON STOP --"); } @Override public void onDestroy() { super.onDestroy(); // Stop the Bluetooth Lock services if (mLockService != null) mLockService.stop(); if(D) Log.e(TAG, "--- ON DESTROY ---"); } @SuppressLint("NewApi")private void ensureDiscoverable() { if(D) Log.d(TAG, "ensure discoverable"); if (mBluetoothAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) { Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); startActivity(discoverableIntent); } } /** * 发送数据. * @param message A string of text to send. */ private void sendMessage(String message) { // Check that we're actually connected before trying anything if (mLockService.getState() != BluetoothLockService.STATE_CONNECTED) { Toast.makeText(this, R.string.not_connected, Toast.LENGTH_SHORT).show(); return; } // Check that there's actually something to send if (message.length() > 0) { // Get the message bytes and tell the BluetoothLockService to write byte[] send = message.getBytes(); mLockService.write(send); // Reset out string buffer to zero and clear the edit text field mOutStringBuffer.setLength(0); mOutEditText.setText(mOutStringBuffer); } } // The action listener for the EditText widget, to listen for the return key @SuppressLint("NewApi")private TextView.OnEditorActionListener mWriteListener = new TextView.OnEditorActionListener() { public boolean onEditorAction(TextView view, int actionId, KeyEvent event) { // If the action is a key-up event on the return key, send the message 回车键发送消息 if (actionId == EditorInfo.IME_NULL && event.getAction() == KeyEvent.ACTION_UP) { String message = view.getText().toString(); message = "&" message "$"; sendMessage(message); } if(D) Log.i(TAG, "END onEditorAction"); return true; } }; /** * *The Handler 获取从BluetoothLockService返回来的信息 只是为了显示 */ private final Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case MESSAGE_STATE_CHANGE: if(D) Log.i(TAG, "MESSAGE_STATE_CHANGE: " msg.arg1); switch (msg.arg1) { case BluetoothLockService.STATE_CONNECTED: mTitle.setText(R.string.title_connected_to); mTitle.append(mConnectedDeviceName); break; case BluetoothLockService.STATE_CONNECTING: mTitle.setText(R.string.title_connecting); break; case BluetoothLockService.STATE_LISTEN: case BluetoothLockService.STATE_NONE: mTitle.setText(R.string.title_not_connected); break; } break; case MESSAGE_WRITE: /* * 发送消息 */ byte[] writeBuf = (byte[]) msg.obj; // construct a string from the buffer String writeMessage = new String(writeBuf); break; case MESSAGE_READ:{ byte[] readBuf = (byte[]) msg.obj; // construct a string from the valid bytes in the buffer String readMessage = new String(readBuf, 0, msg.arg1); //开锁成功 给予提示 //DisplayToast("readMessage:" readMessage); /** * 读取接收到的消息 * * */ //对接收到的数据进行检测 if(readMessage.equals(OPEN_LOCK_SUCCESS)){ //开锁成功 给予提示 DisplayToast("开锁成功"); } else if(readMessage.equals(OPEN_LOCK_FAIL)){ DisplayToast("开锁失败"); } else { DisplayToast("程序运行未知错误"); } } //mConversationArrayAdapter.add(mConnectedDeviceName ": " readMessage); break; case MESSAGE_DEVICE_NAME: // save the connected device's name mConnectedDeviceName = msg.getData().getString(DEVICE_NAME); Toast.makeText(getApplicationContext(), "Connected to " mConnectedDeviceName, Toast.LENGTH_SHORT).show(); break; case MESSAGE_TOAST: Toast.makeText(getApplicationContext(), msg.getData().getString(TOAST), Toast.LENGTH_SHORT).show(); break; } } }; public void DisplayToast(String str) { Toast toast=Toast.makeText(this, str, Toast.LENGTH_LONG); //toast.setGravity(Gravity.TOP, 0, 220); toast.show(); } @SuppressLint("NewApi")public void onActivityResult(int requestCode, int resultCode, Intent data) { if(D) Log.d(TAG, "onActivityResult " resultCode); switch (requestCode) { case REQUEST_CONNECT_DEVICE: // When DeviceListActivity returns with a device to connect if (resultCode == Activity.RESULT_OK) { // Get the device MAC address String address = data.getExtras() .getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS); // Get the BLuetoothDevice object BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); /* /*连接设备 根据地址获取到的device进行连接 * * */ mLockService.connect(device); DisplayToast("已经连接到从机模块"); } break; case REQUEST_ENABLE_BT: // When the request to enable Bluetooth returns if (resultCode == Activity.RESULT_OK) { // 连接成功 进行传送消息 setupLock(); //此时可以修改密码 } else { // User did not enable Bluetooth or an error occured Log.d(TAG, "BT not enabled"); Toast.makeText(this, R.string.bt_not_enabled_leaving, Toast.LENGTH_SHORT).show(); finish(); } } } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.option_menu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.scan: // 激活DeviceListActivity窗体 进行查看和扫描设备 Intent serverIntent = new Intent(this, DeviceListActivity.class); startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE); return true; case R.id.discoverable: // 确保此设备具有可检测性 ensureDiscoverable(); return true; } return false; }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复