UPDATE
我尝试了许多代码,也是从互联网上显示的例子.他们每个都遵循我的方法.经过几个小时的测试,我得出结论,在Android 6.0上没有机会实现未知设备的蓝牙发现,我们只能检索绑定的设备.我很确定这个Android版本有什么东西.
如果有人知道如何解决这个问题,我真的很感激任何帮助.
原帖
我的代码工作正常,但没有找到设备.我只收到DISCOVERY_STARTED和DISCOVERY_FINISHED,因此找不到任何设备,但使用系统应用程序可以找到这些设备.
这是我的应用程序的代码,希望它可以提供帮助.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bluetoothAdapter= BluetoothAdapter.getDefaultAdapter();
//other stuff...
IntentFilter filter=new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(myreceiver,filter);
}
final BroadcastReceiver myreceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.i("test","RECEIVED: "+ action);
if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
}
else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
}
if(BluetoothDevice.ACTION_FOUND.equals(action))
{
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.i("test", device.getName() + "\n" + device.getAddress());
}
}};
public void scanDevices(View v){
if (bluetoothAdapter.isEnabled()){
bluetoothAdapter.startDiscovery();
}
}
Run Code Online (Sandbox Code Playgroud)
我已经设置了权限: …
我正在实现这个类:
#ifndef LIST_H
#define LIST_H
template <typename ListType> class List{
public:
enum ListPosition{LIST_START=-2,LIST_END=-1};
enum Order{ASCENDANT,DESCENDANT};
template <typename NodeType> class ListNode{
public:
ListNode(const NodeType &value,const ListNode<NodeType>*const pElement, const ListNode<NodeType>*const nElement);
~ListNode();
ListNode<NodeType> *const previous() const;
ListNode<NodeType> *const next() const;
void setPrevious(const ListNode<NodeType> *const pElement);
void setNext(const ListNode<NodeType> *const nElement);
void setValue(const NodeType& value);
private:
ListNode<NodeType> *pElement;
ListNode<NodeType> *nElement;
NodeType *value;
};
List();
List(const List<ListType> &list);
~List();
bool contains(const ListType& value) const;
ListType& get(const int pos) const;
ListNode<ListType>& getNode(const int pos) const; …Run Code Online (Sandbox Code Playgroud)