好的,所以目前我的应用程序在我的设备上打开蓝牙,然后列出列表视图中的所有配对设备。我试图这样做,当单击列表视图中的项目时,它将连接到设备(主要用于蓝牙扬声器)。下面我列出了我的主java文件,其中包含运行我的应用程序的所有代码。如果有人能让我朝着正确的方向前进,我就会欣赏它。这也是我的第一个使用蓝牙的项目。
package com.applie.itchaboynathan.appv2;
import android.support.v7.app.AppCompatActivity;
import java.io.IOException;
import java.net.Socket;
import java.util.UUID;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.view.MenuItem;
import android.widget.Toast;
import android.bluetooth.BluetoothAdapter;
import android.widget.SeekBar;
import android.widget.TextView;
import android.content.Intent;
import android.widget.ListView;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import java.util.ArrayList;
import android.widget.ArrayAdapter;
import java.util.Set;
import android.os.Handler;
public class MainActivity extends AppCompatActivity {
private static SeekBar seek_bar;
private static TextView text_view;
private BluetoothAdapter BA;
private Set<BluetoothDevice>pairedDevices;
Handler handler = new Handler();
ListView lv;
ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
volumeBar();
BA = BluetoothAdapter.getDefaultAdapter();
lv = (ListView)findViewById(R.id.deviceList);
iv = (ImageView)findViewById(R.id.imgConnection);
//define menu button
ImageButton menubutton = (ImageButton)findViewById(R.id.ibtnMenu);
//enables the button to show a menu
menubutton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v){
final PopupMenu popupMenu = new PopupMenu(getApplicationContext(),v);
popupMenu.inflate(R.menu.main_menu);
popupMenu.show();
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item){
switch (item.getItemId()) {
case R.id.connect:
//code for connect item
//Delay so the list creates
handler.postDelayed(new Runnable() {
@Override
public void run(){
list();
visible();
iv.setImageResource(R.mipmap.ic_checked);
}
}, 4200);
turnOn();
return true;
case R.id.disconnect:
//code for music item
turnOff();
iv.setImageResource(R.mipmap.ic_disconnected);
listClear();
return true;
case R.id.music:
//code for music item
return true;
case R.id.play:
//code for play item
return true;
case R.id.pause:
//code for pause item
return true;
}
return false;
}
});
}
});//end of button
}
//bluettoth enabler
public void turnOn(){
if (!BA.isEnabled()) {
Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn, 0);
Toast.makeText(getApplicationContext(), "Turned on",Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Already on", Toast.LENGTH_LONG).show();
}
}
//bluetooth disable
public void turnOff(){
BA.disable();
Toast.makeText(getApplicationContext(), "Turned off" ,Toast.LENGTH_LONG).show();
}
public void visible(){
Intent getVisible = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(getVisible, 0);
}
public void listClear(){
lv.setAdapter(null);
}
public void list(){
pairedDevices = BA.getBondedDevices();
ArrayList list = new ArrayList();
for(BluetoothDevice bt : pairedDevices) list.add(bt.getName());
Toast.makeText(getApplicationContext(), "Showing Paired Devices",Toast.LENGTH_SHORT).show();
final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, list);
lv.setAdapter(adapter);
}
//volume bar code
public void volumeBar(){
seek_bar = (SeekBar)findViewById(R.id.seekBar);
text_view =(TextView)findViewById(R.id.txtVolumeLvl);
text_view.setText("Volume : " + seek_bar.getProgress() + " % ");
seek_bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
int progress_value;
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
progress_value = progress;
text_view.setText("Volume : " + progress + " % ");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
text_view.setText("Volume : " + progress_value + " % ");
}
}
);
}
//end volume bar
}
答案 0 :(得分:0)
您已使用BluetoothSocket
连接蓝牙。
使用以下代码进行连接。
private BluetoothAdapter mBluetoothAdarpter;
private BluetoothSocket mBtSocket;
private BluetoothDevice mBTDevice;
private static final UUID SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
..
..
private boolean connectToDevice(String iAdress) {
try {
if(mBluetoothAdarpter == null) mBluetoothAdarpter = BluetoothAdapter.getDefaultAdapter();
mBTDevice = mBluetoothAdarpter.getRemoteDevice(iAdress);
mBtSocket = mBTDevice.createRfcommSocketToServiceRecord(SPP_UUID);
mBtSocket.connect();
return true;
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
答案 1 :(得分:0)
试试这个:
ArrayList list = new ArrayList();
public void list(){
pairedDevices = BA.getBondedDevices();
for(BluetoothDevice bt : pairedDevices) list.add(bt.getName());
Toast.makeText(getApplicationContext(), "Showing Paired Devices",Toast.LENGTH_SHORT).show();
final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, list);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener () {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
String selected = list.get(position);
for (Iterator<BluetoothDevice> it = pairedDevices.iterator(); it.hasNext(); ) {
BluetoothDevice bt = it.next();
if (bt.getName().equals(selected){
//connect to device
}
}
});
}