android-设置蓝牙的可见性

本文介绍了一个简单的蓝牙应用程序,演示了如何使用Android蓝牙API进行设备搜索及设置可见性。通过注册广播接收器来监听蓝牙设备发现的过程,并展示了如何创建按钮监听器以触发蓝牙设备的搜索和设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

老规矩。右键取得图片地址,利用下载工具下载这个显示不出来的图片。后缀改为rar即可得到源代码项目。



package zhang.bluetooth;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
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.view.View.OnClickListener;
import android.widget.Button;

public class bluetooth_02 extends Activity {
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		unregisterReceiver(bluetoothReceiver);
		super.onDestroy();
	}
	private Button set;
	private Button scan;
	private BluetoothReceiver bluetoothReceiver =null;
	private BluetoothAdapter bluetoothAdapter=null;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        set=(Button)findViewById(R.id.set);
        scan=(Button)findViewById(R.id.scan);
        
      set.setOnClickListener(new setListener());
      IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
      bluetoothReceiver = new BluetoothReceiver();
      System.out.println(scan);
      scan.setOnClickListener(new scanListener());
        
    }
    public class BluetoothReceiver extends BroadcastReceiver{
    	@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			String action =intent.getAction();
			if(BluetoothDevice.ACTION_FOUND.equals(action)){
				BluetoothDevice device=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
				System.out.println(device.getAddress());
			}
		}
    }
    class setListener implements OnClickListener{
    	@Override
    	public void onClick(View arg0) {
    		// TODO Auto-generated method stub
    		Intent dis=new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    		dis.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 500);
    		startActivity(dis);
    	}
        }
    class scanListener implements OnClickListener{
    	
        

    	@Override
    	public void onClick(View arg0) {
    		// TODO Auto-generated method stub
    	//与之前的类似
    	}
        }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值