• 大小: 97KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-08
  • 语言: 其他
  • 标签: 蓝牙  

资源简介

安卓蓝牙上位机开发实例,实现发送指令与接收信息功能。 安卓蓝牙上位机开发实例,实现发送指令与接收信息功能。 安卓蓝牙上位机开发实例,实现发送指令与接收信息功能。

资源截图

代码片段和文件信息

package com.test.BTClient;



import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class BTClient extends Activity {

private TextView text0;
    private EditText edit0;
    
  //按钮动作,监听服务端口,打开UUID服务

    BluetoothDevice _device = null;
    BluetoothSocket _socket = null;
    boolean _discoveryFinished = false;

    private BluetoothAdapter _bluetooth = BluetoothAdapter.getDefaultAdapter();



    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        text0 = (TextView)findViewById(R.id.Text0);
        edit0 = (EditText)findViewById(R.id.Edit0);

        
      //打开本地设备
        if (_bluetooth == null){
         Toast.makeText(this “Bluetooth is not available“ Toast.LENGTH_LONG).show();
          //  finish();
            return;
        }
        
        // 设置设备可以被搜索  
        _bluetooth.enable();
        if(_bluetooth.isEnabled()==false){
         Toast.makeText(this “Bluetooth can‘t be discorvered“ Toast.LENGTH_LONG).show();
         //finish();
         return;
        }
        
        
        text0.setText(“打个本地蓝牙服务成功!“);
    }
    

    //定义广播接收器
    private BroadcastReceiver _foundReceiver = new BroadcastReceiver() {
public void onReceive(Context context Intent intent) {
/* 从intent中取得搜索结果数据 */
//BluetoothDevice _device0 = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//if (_device0.getName().equals(“linvor“) )
_device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);;
text0.setText(“找寻到设备:“+_device.getName());
}
};
private BroadcastReceiver _discoveryReceiver = new BroadcastReceiver() {

@Override
public void onReceive(Context context Intent intent) 
{
/* 卸载注册的接收器 */
unregisterReceiver(_foundReceiver);
unregisterReceiver(this);
_discoveryFinished = true;
}
};
    
    //连接搜寻服务
public void onSearchButtonclicked(View v){

IntentFilter discoveryFilter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(_discoveryReceiver discoveryFilter);
IntentFilter foundFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(_foundReceiver foundFilter);
new Thread(){
     public void run() 
     {
     //开始搜索 
     _bluetooth.startDiscovery();
     for (;;) 
     {
     if (_discoveryFinished) 
     {
     break;
     }
     try 
     {
     Thread.sleep(10

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-06-05 14:51  BTClient\
     文件         475  2018-05-26 08:23  BTClient\.classpath
     文件         844  2010-12-01 16:58  BTClient\.project
     目录           0  2018-06-05 14:51  BTClient\.settings\
     文件         177  2018-05-26 08:22  BTClient\.settings\org.eclipse.jdt.core.prefs
     文件        1178  2011-01-28 13:38  BTClient\AndroidManifest.xml
     目录           0  2018-06-05 22:27  BTClient\assets\
     目录           0  2018-06-05 14:51  BTClient\backup\
     文件        4558  2010-12-11 17:00  BTClient\backup\BTClient.java
     目录           0  2018-06-05 22:16  BTClient\bin\
     文件        1178  2018-06-05 22:15  BTClient\bin\AndroidManifest.xml
     文件       31605  2018-06-05 22:16  BTClient\bin\BTClient.apk
     目录           0  2018-06-05 22:15  BTClient\bin\classes\
     目录           0  2018-06-05 22:15  BTClient\bin\classes\com\
     目录           0  2018-06-05 22:15  BTClient\bin\classes\com\test\
     目录           0  2018-06-05 22:15  BTClient\bin\classes\com\test\BTClient\
     文件        2145  2018-06-05 22:15  BTClient\bin\classes\com\test\BTClient\BTClient$1.class
     文件        1093  2018-06-05 22:15  BTClient\bin\classes\com\test\BTClient\BTClient$2.class
     文件         819  2018-06-05 22:15  BTClient\bin\classes\com\test\BTClient\BTClient$3.class
     文件        6008  2018-06-05 22:15  BTClient\bin\classes\com\test\BTClient\BTClient.class
     文件         343  2018-06-05 22:15  BTClient\bin\classes\com\test\BTClient\BuildConfig.class
     文件        1912  2018-06-05 22:15  BTClient\bin\classes\com\test\BTClient\DeviceListActivity$1.class
     文件        2225  2018-06-05 22:15  BTClient\bin\classes\com\test\BTClient\DeviceListActivity$2.class
     文件         875  2018-06-05 22:15  BTClient\bin\classes\com\test\BTClient\DeviceListActivity$3.class
     文件        4280  2018-06-05 22:15  BTClient\bin\classes\com\test\BTClient\DeviceListActivity.class
     文件         337  2018-06-05 22:16  BTClient\bin\classes\com\test\BTClient\R$attr.class
     文件         397  2018-06-05 22:16  BTClient\bin\classes\com\test\BTClient\R$drawable.class
     文件         898  2018-06-05 22:16  BTClient\bin\classes\com\test\BTClient\R$id.class
     文件         521  2018-06-05 22:16  BTClient\bin\classes\com\test\BTClient\R$layout.class
     文件         392  2018-06-05 22:16  BTClient\bin\classes\com\test\BTClient\R$menu.class
     文件         424  2018-06-05 22:16  BTClient\bin\classes\com\test\BTClient\R$string.class
............此处省略46个文件信息

评论

共有 条评论