gpt4 book ai didi

java - 创建一个监听器,从 BroadcastReceiver 传递值

转载 作者:行者123 更新时间:2023-12-02 00:28:17 25 4
gpt4 key购买 nike

我对如何执行一项操作有些疑问。

当接到一个收入电话时,我想将我的类(class)的号码传递给它,它管理与 Arduino 的蓝牙连接。

发送号码后,我想通过蓝牙连接发送

我尝试使用 Intent ,但也许我使用的是已弃用的版本。

Android API 7.1

广播接收器类

package com.jidea.glass;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

public class TelephonyReceiver extends BroadcastReceiver
{

@Override
public void onReceive(Context arg0, Intent intent) {
// TODO Auto-generated method stub
try {
if (intent != null && intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
//Toast.makeText(context, "Outgoign call", 1000).show();
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
} else {
//get the phone state
String newPhoneState = intent.hasExtra(TelephonyManager.EXTRA_STATE) ? intent.getStringExtra(TelephonyManager.EXTRA_STATE) : null;
Bundle bundle = intent.getExtras();

if (newPhoneState != null && newPhoneState.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
//read the incoming call number
String phoneNumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.i("PHONE RECEIVER", "Telephone is now ringing " + phoneNumber);
//Toast.makeText(arg0, "Tele disponivel " + phoneNumber, Toast.LENGTH_LONG).show();
if(phoneNumber!=null | phoneNumber.equals("")){
PASS HERE TO BLUETOOTH
}
}

蓝牙类

package com.jidea.glass;


import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
//import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

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

public class CustomProcess extends AppCompatActivity
{
String address = null;
private ProgressDialog progress;
BluetoothAdapter myBluetooth = null;
BluetoothSocket btSocket = null;
private boolean isBtConnected = false;
//SPP UUID. Look for it
static final UUID myUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

//observable


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent newint = getIntent();
address = newint.getStringExtra(MainActivity.EXTRA_ADDRESS); //receive the address of the bluetooth device
setContentView(R.layout.activity_custom_process);
//msg(address);
new ConnectBT().execute(); //Call the class to connect

}
public void isConnected() {//View view
if (btSocket!=null)
{
try
{
btSocket.getOutputStream().write("Conectado".toString().getBytes());
}
catch (IOException e)
{
msg("Error");
}
}
}

public void haveCall(String Phone) {//View view
Object aLig="Ligação \n"+Phone;
if (btSocket!=null)
{
try
{
btSocket.getOutputStream().write(aLig.toString().getBytes());
}
catch (IOException e)
{
msg("Error");
}
}
}

private void msg(String s)
{
Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();
}

private class ConnectBT extends AsyncTask<Void, Void, Void> // UI thread
{
private boolean ConnectSuccess = true; //if it's here, it's almost connected

@Override
protected void onPreExecute()
{
progress = ProgressDialog.show(CustomProcess.this, "Connecting...", "Please wait!!!"); //show a progress dialog
}

@Override
protected Void doInBackground(Void... devices) //while the progress dialog is shown, the connection is done in background
{
try
{
if (btSocket == null || !isBtConnected)
{
myBluetooth = BluetoothAdapter.getDefaultAdapter();//get the mobile bluetooth device
BluetoothDevice dispositivo = myBluetooth.getRemoteDevice(address);//connects to the device's address and checks if it's available
btSocket = dispositivo.createInsecureRfcommSocketToServiceRecord(myUUID);//create a RFCOMM (SPP) connection
BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
btSocket.connect();//start connection
}
}
catch (IOException e)
{
ConnectSuccess = false;//if the try failed, you can check the exception here
}
return null;
}
@Override
protected void onPostExecute(Void result) //after the doInBackground, it checks if everything went fine
{
super.onPostExecute(result);

if (!ConnectSuccess)
{
msg("Connection Failed. Is it a SPP Bluetooth? Try again.");
finish();
}
else
{
msg("Conectado");
isBtConnected = true;
isConnected();
}
progress.dismiss();
}
}


}

最佳答案

首先,您应该在 BroadcastReceiver 中创建一个 Intent ,然后使用 LocalBroadcastManager 以便能够将该 Intent 发送到您想要收听的任何地方。

val intent = Intent(ACTION)
intent.putExtra("DATA KEY", DATA)
LocalBroadcastManager.getInstance(context).sendBroadcast(intent)

不要忘记在您想收听的地方注册您的广播接收器

LocalBroadcastManager.getInstance(context).registerReceiver(your broadcast receiver instance, IntentFilter(ACTION))

关于java - 创建一个监听器,从 BroadcastReceiver 传递值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58036841/

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