gpt4 book ai didi

java - 以编程方式扫描蓝牙设备在 Android 6+ 中找不到任何内容

转载 作者:行者123 更新时间:2023-12-02 09:37:59 27 4
gpt4 key购买 nike

我创建了一个将手机与蓝牙模块配对的应用程序。我用 4.2 Android 手机测试了这个应用程序,它运行得很好,但在 Android 版本高于 6 的手机中,当我按下“扫描新设备”按钮时,设备没有找到任何东西。但是当我检查手机蓝牙本身时,我可以找到我正在寻找的模块..

蓝牙连接类别:

public class BluetoothConnectionActivity extends AppCompatActivity {

private ProgressDialog mProgressDlg;

private ArrayList<BluetoothDevice> mDeviceList = new ArrayList<BluetoothDevice>();

private BluetoothAdapter mBluetoothAdapter;
BluetoothDevice device ;

private MediaPlayer mediaplayer = null ;

private Button bluetoothOnOff;
private Button viewpairedDevices;
private Button scanfornewDevices;
private Button blue2;


ImageView unMute;

View view1;
View view2;
private Activity context;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bluetoothconnectionlayout);


bluetoothOnOff = (Button) findViewById(R.id.bluetoothOnOff);
viewpairedDevices = (Button) findViewById(R.id.viewpairedDevicesButton);
scanfornewDevices = (Button) findViewById(R.id.scanfornewDevicesButton);
blue2 = (Button) findViewById(R.id.bluetoothOnOff2);
view1 = findViewById(R.id.viewFader);
view2 = findViewById(R.id.viewFader2);
unMute = (ImageView) findViewById(R.id.settingStartupSecondary);


mediaplayer = MediaPlayer.create(getApplicationContext(),R.raw.sfxturnon);







Handler startup1 = new Handler();
startup1.postDelayed(new Runnable() {
@Override
public void run() {

view1.animate().alpha(1f).setDuration(500);
view2.animate().alpha(1f).setDuration(500);


}
},1000);



Handler startup2 = new Handler();
startup2.postDelayed(new Runnable() {
@Override
public void run() {

bluetoothOnOff.animate().alpha(1f).setDuration(500);
viewpairedDevices.animate().alpha(1f).setDuration(500);
scanfornewDevices.animate().alpha(1f).setDuration(500);
unMute.animate().alpha(1f).setDuration(100);


}
},1000);


//
// settingBtn.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
//
//
// AudioManager amanager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
// amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, true);
//
//// settingBtn.setEnabled(false);
//// settingBtn.animate().alpha(0f);
//
// unMute.animate().alpha(1f);
// unMute.setEnabled(true);
//
//
// Toast.makeText(getApplicationContext(), " Application muted. ", Toast.LENGTH_SHORT).show();
//
//
// }
// });










Handler dawg = new Handler();
dawg.postDelayed(new Runnable() {
@Override
public void run() {

bluetoothOnOff.animate().alpha(1f).setDuration(1500);
viewpairedDevices.animate().alpha(1f).setDuration(2500);
scanfornewDevices.animate().alpha(1f).setDuration(3000);



}
},500);












mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();



mProgressDlg = new ProgressDialog(this);

mProgressDlg.setMessage("Scanning, Please wait...");
mProgressDlg.setCancelable(false);
mProgressDlg.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();

mBluetoothAdapter.cancelDiscovery();
}
});}








if (mBluetoothAdapter.isEnabled()){
bluetoothOnOff.setText("Bluetooth is On");
blue2.setText("Bluetooth is On");
}else {

blue2.setText("Bluetooth is Off");
bluetoothOnOff.setText("Bluetooth is Off");

}


if (mBluetoothAdapter == null) {
showUnsupported();
} else {
viewpairedDevices.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {






Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();

if (pairedDevices == null || pairedDevices.size() == 0) {
Toast toast = Toast.makeText(getApplicationContext()," No paired device found.", Toast.LENGTH_SHORT);
View view3 = toast.getView();
view3.setBackgroundColor(Color.parseColor("#ffff5b"));
TextView text = (TextView) view3.findViewById(android.R.id.message);

text.setTextColor(Color.parseColor("#000000"));
toast.show();
} else {
ArrayList<BluetoothDevice> list = new ArrayList<BluetoothDevice>();

list.addAll(pairedDevices);

Intent intent = new Intent(BluetoothConnectionActivity.this, DeviceList.class);

intent.putParcelableArrayListExtra("device.list", list);

startActivity(intent);
}
}
});

scanfornewDevices.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {



mBluetoothAdapter.startDiscovery();

}
});






bluetoothOnOff.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {


if (mBluetoothAdapter.isEnabled()) {
mBluetoothAdapter.disable();
showDisabled();



bluetoothOnOff.setText("Bluetooth is OFF");




final Handler handler6 = new Handler();
handler6.postDelayed(new Runnable() {
@Override
public void run() {


mediaplayer.start();


}
}, 500);



final Handler handler3 = new Handler();
handler3.postDelayed(new Runnable() {
@Override
public void run() {



bluetoothOnOff.animate().translationY(-1f).setDuration(5000);
blue2.animate().translationY(-1f).setDuration(5000);

bluetoothOnOff.animate().alpha(1f).setDuration(3000);
blue2.animate().alpha(0f).setDuration(3000);






}
}, 500);



} else {
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

startActivityForResult(intent, 1000);






}}
});


blue2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {


if (mBluetoothAdapter.isEnabled()) {
mBluetoothAdapter.disable();
showDisabled();



bluetoothOnOff.setText("Bluetooth is OFF");





final Handler handler6 = new Handler();
handler6.postDelayed(new Runnable() {
@Override
public void run() {


mediaplayer.start();


}
}, 500);



final Handler handler3 = new Handler();
handler3.postDelayed(new Runnable() {
@Override
public void run() {



bluetoothOnOff.animate().translationY(-1f).setDuration(5000);
blue2.animate().translationY(-1f).setDuration(5000);

bluetoothOnOff.animate().alpha(1f).setDuration(3000);
blue2.animate().alpha(0f).setDuration(3000);






}
}, 500);



} else {
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

startActivityForResult(intent, 1000);






}}
});





if (mBluetoothAdapter.isEnabled()) {



showEnabled();
} else {

showDisabled();
}
}


IntentFilter filter = new IntentFilter();

filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);

registerReceiver(mReceiver, filter);

}































@Override
public void onPause() {
if (mBluetoothAdapter != null) {
if (mBluetoothAdapter.isDiscovering()) {
mBluetoothAdapter.cancelDiscovery();
}
}

super.onPause();
}

@Override
public void onBackPressed() {


}

@Override
public void onDestroy() {
unregisterReceiver(mReceiver);

super.onDestroy();
}

private void showEnabled() {



bluetoothOnOff.setEnabled(true);

viewpairedDevices.setEnabled(true);
viewpairedDevices.setBackgroundResource(R.drawable.selectorstylish);
scanfornewDevices.setEnabled(true);
scanfornewDevices.setBackgroundResource(R.drawable.selectorstylish);
}

private void showDisabled() {




bluetoothOnOff.setEnabled(true);

viewpairedDevices.setEnabled(false);
viewpairedDevices.setBackgroundResource(R.drawable.selectorstylish);
scanfornewDevices.setEnabled(false);
scanfornewDevices.setBackgroundResource(R.drawable.selectorstylish);
}

private void showUnsupported() {
Toast toast = Toast.makeText(getApplicationContext()," Bluetooth is not supported by this device.", Toast.LENGTH_SHORT);
View view3 = toast.getView();
view3.setBackgroundColor(Color.parseColor("#ffff5b"));
TextView text = (TextView) view3.findViewById(android.R.id.message);

text.setTextColor(Color.parseColor("#000000"));
toast.show();


bluetoothOnOff.setEnabled(false);
viewpairedDevices.setEnabled(false);
viewpairedDevices.setBackgroundResource(R.drawable.selectorstylish);
scanfornewDevices.setEnabled(false);
scanfornewDevices.setBackgroundResource(R.drawable.selectorstylish);

}

private void showToast(String message) {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
}

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();


if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);

if (state == BluetoothAdapter.STATE_ON) {




bluetoothOnOff.setText("Bluetooth is On");
blue2.setText("Bluetooth is On");



showEnabled();



final Handler handler5 = new Handler();
handler5.postDelayed(new Runnable() {
@Override
public void run() {


mediaplayer.start();


}
}, 500);



final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {



bluetoothOnOff.animate().translationY(150f).setDuration(5000);
blue2.animate().translationY(150f).setDuration(5000);


bluetoothOnOff.animate().alpha(0f).setDuration(3000);
blue2.animate().alpha(1f).setDuration(3000);





}
}, 500);


}







} else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
mDeviceList = new ArrayList<BluetoothDevice>();

mProgressDlg.show();
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
mProgressDlg.dismiss();

// Intent newIntent = new Intent(BluetoothConnectionActivity.this, DeviceList.class);
//
// newIntent.putParcelableArrayListExtra("device.list", mDeviceList);
//
// startActivity(newIntent);
} else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

mDeviceList.add(device);

Toast toast = Toast.makeText(getApplicationContext()," Found device" + device.getName(), Toast.LENGTH_SHORT);
View view3 = toast.getView();
view3.setBackgroundColor(Color.parseColor("#ffff5b"));
TextView text = (TextView) view3.findViewById(android.R.id.message);

text.setTextColor(Color.parseColor("#000000"));
toast.show();
}
}
};


@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.optionmenu,menu);
return super.onCreateOptionsMenu(menu);





}
}

设备列表类别:

public class DeviceList extends AppCompatActivity {

private ListView mListView;
private AdapterClassBluetooth mAdapterClassBluetooth;
private ArrayList<BluetoothDevice> mDeviceList;
private BluetoothDevice device ;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_device_list);





mDeviceList = getIntent().getExtras().getParcelableArrayList("device.list");

mListView = (ListView) findViewById(R.id.lv_paired);

mAdapterClassBluetooth = new AdapterClassBluetooth(this);

mAdapterClassBluetooth.setData(mDeviceList);
mAdapterClassBluetooth.setListener(new AdapterClassBluetooth.OnPairButtonClickListener() {
@Override
public void onPairButtonClick(int position) {
BluetoothDevice device = mDeviceList.get(position);

if (device.getBondState() == BluetoothDevice.BOND_BONDED) {

unpairDevice(device);
} else {
Toast toast = Toast.makeText(getApplicationContext(), "Pairing..." , Toast.LENGTH_SHORT);
View view = toast.getView();
view.setBackgroundColor(Color.parseColor("#ffff5b"));
TextView text = (TextView) view.findViewById(android.R.id.message);

text.setTextColor(Color.parseColor("#000000"));
toast.show();

pairDevice(device);
}
}
});

mListView.setAdapter(mAdapterClassBluetooth);


registerReceiver(mPairReceiver, new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED));













}

@Override
public void onDestroy() {
unregisterReceiver(mPairReceiver);

super.onDestroy();
}


private void showToast(String message) {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
}

private void pairDevice(BluetoothDevice device) {
try {
Method method = device.getClass().getMethod("createBond", (Class[]) null);
method.invoke(device, (Object[]) null);
} catch (Exception e) {
e.printStackTrace();
}
}

private void unpairDevice(BluetoothDevice device) {
try {
Method method = device.getClass().getMethod("removeBond", (Class[]) null);
method.invoke(device, (Object[]) null);

} catch (Exception e) {
e.printStackTrace();
}
}

private final BroadcastReceiver mPairReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();

if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
final int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR);
final int prevState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, BluetoothDevice.ERROR);

if (state == BluetoothDevice.BOND_BONDED && prevState == BluetoothDevice.BOND_BONDING) {




Toast toast = Toast.makeText(getApplicationContext()," Device are paired, please wait...", Toast.LENGTH_SHORT);
View view = toast.getView();
view.setBackgroundColor(Color.parseColor("#ffff5b"));
TextView text = (TextView) view.findViewById(android.R.id.message);

text.setTextColor(Color.parseColor("#000000"));
toast.show();





Handler delaying = new Handler();
delaying.postDelayed(new Runnable() {
@Override
public void run() {

Intent communicate = new Intent(DeviceList.this,Security.class);
startActivity(communicate);
finish();

}
},1500);








} else if (state == BluetoothDevice.BOND_NONE && prevState == BluetoothDevice.BOND_BONDED){
Toast toast = Toast.makeText(getApplicationContext(),"Unpaired.", Toast.LENGTH_SHORT);
View view = toast.getView();
view.setBackgroundColor(Color.parseColor("#ffff5b"));
TextView text = (TextView) view.findViewById(android.R.id.message);

text.setTextColor(Color.parseColor("#000000"));
toast.show();
}

mAdapterClassBluetooth.notifyDataSetChanged();
}
}

};}

适配器类:

public class AdapterClassBluetooth extends BaseAdapter{

private LayoutInflater mInflater;
private List<BluetoothDevice> mData;
private OnPairButtonClickListener mListener;

public AdapterClassBluetooth(){


}

public AdapterClassBluetooth(Context context) {
super();
mInflater = LayoutInflater.from(context);
}

public void setData(List<BluetoothDevice> data) {
mData = data;
}

public void setListener(OnPairButtonClickListener listener) {
mListener = listener;
}

public int getCount() {
return (mData == null) ? 0 : mData.size();
}

public Object getItem(int position) {
return null;
}

public long getItemId(int position) {
return position;
}

public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;

if (convertView == null) {
convertView = mInflater.inflate(R.layout.adapterlist, null);

holder = new ViewHolder();

holder.nameTv = (TextView) convertView.findViewById(R.id.tv_name);
holder.addressTv = (TextView) convertView.findViewById(R.id.tv_address);
holder.pairBtn = (Button) convertView.findViewById(R.id.btn_pair);

convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

BluetoothDevice device = mData.get(position);

holder.nameTv.setText(device.getName());
holder.addressTv.setText(device.getAddress());
holder.pairBtn.setText((device.getBondState() == BluetoothDevice.BOND_BONDED) ? "Unpair" : "Pair");
holder.pairBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mListener != null) {
mListener.onPairButtonClick(position);
}
}
});


return convertView;
}

static class ViewHolder {
TextView nameTv;
TextView addressTv;
TextView pairBtn;
}

public interface OnPairButtonClickListener {
public abstract void onPairButtonClick(int position);
}




}

在一篇文章中,我研究了这个问题,并通过以下代码发现了这一点:

 if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(context,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
1);

我可以使应用程序支持 Android 6 以上的手机,这样我就可以发现附近的所有设备并与它们配对,

问题是,我不知道应该在哪里使用这段代码。如果您能告诉我在此处显示的代码中如何使用它,我将非常感激。

或者只是告诉我如何使用您可能知道的任何其他代码来克服这个问题。

最佳答案

在您的 AndroidManifest.xml 文件中添加以下内容:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

然后,不调用 registerReceiver,而是调用此函数:

private tryRegisterReceiver() {    
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// Here, permission is not granted
// after calling requestPermissions, the result must be treated in onRequestPermissionResult
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
1000); // 100 or other desired code
} else {
// Permission has already been granted
// continue registering your receiver
registerReceiver();
}
}

处理权限请求的结果

@Override
public void onRequestPermissionsResult(int requestCode,
String[] permissions, int[] grantResults) {
switch (requestCode) {
case 1000: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted
// continue registering your receiver
registerReceiver();
} else {
// permission denied
// show a message or finish your activity
}
return;
}

// other 'case' for other permissions if needed
}
}

关于java - 以编程方式扫描蓝牙设备在 Android 6+ 中找不到任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57342296/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com